# Simple code

**URL:** <https://www.ruby-forum.com/t/simple-code/117706>\
**Category:** Ruby\
**Created:** [October 19, 2007, 3:45pm UTC](https://www.ruby-forum.com/t/simple-code/117706 "2007-10-19T15:45:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 19, 2007, 3:45pm UTC](https://www.ruby-forum.com/t/simple-code/117706/1 "2007-10-19T15:45:43Z")

</div>

hi

#!/usr/bin/ruby -w  
re=Regexp.new(’\s+[A-Z]’)  
puts " {| class=“wikitable”  
|-  
Table – put your title here  
|-"  
while line = gets  
md=re.match(line)  
if md =~ nil  
else  
puts md  
end  
end

I input the text as

COM - Commercial  
EDU - Educational  
GOV - Povernment  
NET - Network  
MIL - Military  
ORG - Non-profit organisations

and I get the result as

{| class=“wikitable”  
|-  
Table – put your title here  
|-  
nil  
C  
E  
G  
N  
M  
O  
nil

How do I prevent the nil appearing?  
How do I access each matchdata with the postmatchdata on each line?

DavidR

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 19, 2007, 3:52pm UTC](https://www.ruby-forum.com/t/simple-code/117706/2 "2007-10-19T15:52:47Z")

</div>

On Oct 19, 2007, at 09:45, removed\_email\_address@domain.invalid wrote:

> while line = gets  
> md=re.match(line)  
> if md =~ nil

Try “if md.nil?” instead.

> else  
> puts md

puts md, md.post\_match

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 19, 2007, 8:13pm UTC](https://www.ruby-forum.com/t/simple-code/117706/3 "2007-10-19T20:13:37Z")

</div>

Ben G. wrote:

> On Oct 19, 2007, at 09:45, removed\_email\_address@domain.invalid wrote:
> 
> > while line = gets  
> > md=re.match(line)  
> > if md =~ nil
> 
> Try “if md.nil?” instead.

if md  
#do stuff

> > else  
> > puts md
> 
> puts md, md.post\_match

puts line

Try this:

re=Regexp.new(’^\s+[A-Z]’) #match start of line only  
…  
…  
while line = gets  
md=re.match(line)

if md  
puts line  
end

end

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 20, 2007, 3:20am UTC](https://www.ruby-forum.com/t/simple-code/117706/4 "2007-10-20T03:20:27Z")

</div>

On Oct 19, 8:44 am, “removed\_email\_address@domain.invalid”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> if md =~ nil  
> NET - Network  
> C  
> DavidR  
> re = /\s+[A-Z]/

puts ’ {| class=“wikitable”  
|-  
Table – put your title here  
|-’

while line = gets  
puts line if line =~ re  
end

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 20, 2007, 3:31am UTC](https://www.ruby-forum.com/t/simple-code/117706/5 "2007-10-20T03:31:01Z")

</div>

On Oct 19, 8:44 am, “removed\_email\_address@domain.invalid”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> if md =~ nil  
> NET - Network  
> C  
> DavidR  
> #!awk -f

BEGIN {  
print " {| class="wikitable"  
|-  
Table – put your title here  
|-" }

/[\t]+[A-Z]/
