# Simple code

**URL:** <https://rubytalk.org/t/simple-code/41491>\
**Category:** ruby-talk\
**Created:** [19 October 2007 13:45 UTC](https://rubytalk.org/t/simple-code/41491 "2007-10-19T13:45:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![angel.of.north](https://avatars.discourse-cdn.com/v4/letter/a/54ee81/32.png) [@angel.of.north](https://rubytalk.org/u/angel.of.north)\
**Post date:** [19 October 2007 13:45 UTC](https://rubytalk.org/t/simple-code/41491/1 "2007-10-19T13:45:02Z")

</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)  
&nbsp;&nbsp;if md =~ nil  
&nbsp;&nbsp;else  
&nbsp;&nbsp;puts md  
&nbsp;&nbsp;end  
end

I input the text as

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

and I get the result as

{| class="wikitable"

> -

Table -- put your title here

> -

nil  
&nbsp;&nbsp;&nbsp;C  
&nbsp;&nbsp;&nbsp;E  
&nbsp;&nbsp;&nbsp;G  
&nbsp;&nbsp;&nbsp;N  
&nbsp;&nbsp;&nbsp;M  
&nbsp;&nbsp;&nbsp;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:** ![Ben\_Giddings1](https://avatars.discourse-cdn.com/v4/letter/b/779978/32.png) [@Ben\_Giddings1](https://rubytalk.org/u/Ben_Giddings1)\
**Post date:** [19 October 2007 13:51 UTC](https://rubytalk.org/t/simple-code/41491/2 "2007-10-19T13:51:26Z")

</div>

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

Try "if md.nil?" instead.

> &nbsp;&nbsp;else  
> &nbsp;&nbsp;puts md

puts md, md.post\_match

> **···**
>
> On Oct 19, 2007, at 09:45, angel.of.north@googlemail.com wrote:
> 
> > &nbsp;&nbsp;end  
> > end

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [20 October 2007 01:20 UTC](https://rubytalk.org/t/simple-code/41491/3 "2007-10-20T01:20:02Z")

</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)  
> &nbsp;&nbsp;if md =~ nil  
> &nbsp;&nbsp;else  
> &nbsp;&nbsp;puts md  
> &nbsp;&nbsp;end  
> end
> 
> I input the text as
> 
> &nbsp;&nbsp;&nbsp;COM - Commercial  
> &nbsp;&nbsp;&nbsp;EDU - Educational  
> &nbsp;&nbsp;&nbsp;GOV - Povernment  
> &nbsp;&nbsp;&nbsp;NET - Network  
> &nbsp;&nbsp;&nbsp;MIL - Military  
> &nbsp;&nbsp;&nbsp;ORG - Non-profit organisations
> 
> and I get the result as
> 
> {| class="wikitable"  
> \>-  
> Table -- put your title here  
> \>-  
> nil  
> &nbsp;&nbsp;&nbsp;C  
> &nbsp;&nbsp;&nbsp;E  
> &nbsp;&nbsp;&nbsp;G  
> &nbsp;&nbsp;&nbsp;N  
> &nbsp;&nbsp;&nbsp;M  
> &nbsp;&nbsp;&nbsp;O  
> nil
> 
> How do I prevent the nil appearing?  
> How do I access each matchdata with the postmatchdata on each line?
> 
> DavidR

re = /\s+[A-Z]/

puts ' {| class="wikitable"

> -

Table -- put your title here

> -'

while line = gets  
&nbsp;&nbsp;puts line if line =~ re  
end

> **···**
>
> On Oct 19, 8:44 am, "angel.of.no...@googlemail.com" \<angel.of.no...@googlemail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [20 October 2007 01:30 UTC](https://rubytalk.org/t/simple-code/41491/4 "2007-10-20T01:30:02Z")

</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)  
> &nbsp;&nbsp;if md =~ nil  
> &nbsp;&nbsp;else  
> &nbsp;&nbsp;puts md  
> &nbsp;&nbsp;end  
> end
> 
> I input the text as
> 
> &nbsp;&nbsp;&nbsp;COM - Commercial  
> &nbsp;&nbsp;&nbsp;EDU - Educational  
> &nbsp;&nbsp;&nbsp;GOV - Povernment  
> &nbsp;&nbsp;&nbsp;NET - Network  
> &nbsp;&nbsp;&nbsp;MIL - Military  
> &nbsp;&nbsp;&nbsp;ORG - Non-profit organisations
> 
> and I get the result as
> 
> {| class="wikitable"  
> \>-  
> Table -- put your title here  
> \>-  
> nil  
> &nbsp;&nbsp;&nbsp;C  
> &nbsp;&nbsp;&nbsp;E  
> &nbsp;&nbsp;&nbsp;G  
> &nbsp;&nbsp;&nbsp;N  
> &nbsp;&nbsp;&nbsp;M  
> &nbsp;&nbsp;&nbsp;O  
> nil
> 
> How do I prevent the nil appearing?  
> How do I access each matchdata with the postmatchdata on each line?
> 
> DavidR

#!awk -f

BEGIN {  
&nbsp;&nbsp;print " {| class=\"wikitable\"\

> -\

Table -- put your title here\

> -" }

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

> **···**
>
> On Oct 19, 8:44 am, "angel.of.no...@googlemail.com" \<angel.of.no...@googlemail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [19 October 2007 18:13 UTC](https://rubytalk.org/t/simple-code/41491/5 "2007-10-19T18:13:40Z")

</div>

Ben Giddings wrote:

> > while line = gets  
> > md=re.match(line)  
> > &nbsp;&nbsp;if md =~ nil
> 
> Try "if md.nil?" instead.

if md  
&nbsp;&nbsp;#do stuff

> > &nbsp;&nbsp;else  
> > &nbsp;&nbsp;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  
&nbsp;&nbsp;md=re.match(line)

&nbsp;&nbsp;if md  
&nbsp;&nbsp;&nbsp;&nbsp;puts line  
&nbsp;&nbsp;end

end

> **···**
>
> > On Oct 19, 2007, at 09:45, angel.of.north@googlemail.com wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
