# Regular expressions and conditionals

**URL:** <https://www.ruby-forum.com/t/regular-expressions-and-conditionals/151795>\
**Category:** Ruby\
**Created:** [October 25, 2008, 10:24pm UTC](https://www.ruby-forum.com/t/regular-expressions-and-conditionals/151795 "2008-10-25T22:24:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bouldr-matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85e7bf/32.png) [@bouldr-matt](https://www.ruby-forum.com/u/bouldr-matt)\
**Post date:** [October 25, 2008, 10:24pm UTC](https://www.ruby-forum.com/t/regular-expressions-and-conditionals/151795/1 "2008-10-25T22:24:46Z")

</div>

I’m writting a little program to convert some source files to html.

I’m using regexp’s to determine the content of each line and format it  
accordingly. For example, if the line starts with a #, then it should be  
output with a div (and id set to ‘comment’).

The problem is with matching the regular expressions. If I do this:

f = File.new(“myfile”, “r”)  
while !f.eof?  
line = f.gets

if line.match(/^# (._)/)  
puts "_

_#{line.match(/^# (.)/)[0]}_
_"  
end  
end_

Then not only do I get the commen lines formatted properly, I also get a  
“nil” for every other line in the file.

How should I be using regexps in conditionals? I’m sure this should be  
easy  
but I’ve googled around for an hour and I can’t find what’s wrong.

TIA

Matt

---

<div class="post-metadata">

**Author:** ![bouldr-matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85e7bf/32.png) [@bouldr-matt](https://www.ruby-forum.com/u/bouldr-matt)\
**Post date:** [October 26, 2008, 1:02am UTC](https://www.ruby-forum.com/t/regular-expressions-and-conditionals/151795/2 "2008-10-26T01:02:53Z")

</div>

On 10/25/08, Matt H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> line = f.gets
> 
> if line.match(/^# (._)/)  
> puts "_
> 
> _#{line.match(/^# (._)/)[0]}
> "  
> end  
> end

I think what you’re trying to write is a little more like this:  
f = File.new(“myfile”, “r”)  
while line = f.gets  
if line.match(/^# (.\*)/)  
line=“

#{line.chomp}
\n”  
end  
puts line  
end

> Then not only do I get the commen lines formatted properly, I also get a  
> “nil” for every other line in the file.

I didn’t see any nils when I ran it… it just didn’t print anything  
but comments. Which would be because your original only printed lines  
back out if they matched your comment pattern.

Why do you have a space after the # in your pattern? I’ve found it to  
be good luck to always escape # when in regexps, even if it isn’t  
followed by a {. Probably a bug lurking in there.

---

<div class="post-metadata">

**Author:** ![bouldr-matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85e7bf/32.png) [@bouldr-matt](https://www.ruby-forum.com/u/bouldr-matt)\
**Post date:** [October 26, 2008, 10:44am UTC](https://www.ruby-forum.com/t/regular-expressions-and-conditionals/151795/3 "2008-10-26T10:44:03Z")

</div>

while line = f.gets  
case line  
when /^# _(._)/  
puts “

#{$1}
”  
else  
puts line  
end  
end

---

<div class="post-metadata">

**Author:** ![bouldr-matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85e7bf/32.png) [@bouldr-matt](https://www.ruby-forum.com/u/bouldr-matt)\
**Post date:** [October 26, 2008, 11:35am UTC](https://www.ruby-forum.com/t/regular-expressions-and-conditionals/151795/4 "2008-10-26T11:35:40Z")

</div>

On 26.10.2008 01:02, Caleb C. wrote:

> > while !f.eof?  
> > while line = f.gets  
> > if line.match(/^# (.\*)/)  
> > line="
> > 
> > #{line.chomp}
> > \n"  
> > end  
> > puts line  
> > end

Even better is

# Use the block form of File.open!

File.new(“myfile”, “r”) do |f|  
f.each do |line|  
line.chomp!  
case line  
when /^# _(._)/  
puts “

# {$1}
”  
else  
puts line  
end  
end  
end

or, for that matter

File.foreach(“myfile”) do |line|  
line.chomp!  
case line  
when /^# _(._)/  
puts “

# {$1}
”  
else  
puts line  
end  
end

Kind regards

robert
