# Regex or line.each seems to not be working

**URL:** <https://www.ruby-forum.com/t/regex-or-line-each-seems-to-not-be-working/182475>\
**Category:** Ruby\
**Created:** [January 28, 2010, 6:08pm UTC](https://www.ruby-forum.com/t/regex-or-line-each-seems-to-not-be-working/182475 "2010-01-28T18:08:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![oshybrid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c68b51/32.png) [@oshybrid](https://www.ruby-forum.com/u/oshybrid)\
**Post date:** [January 28, 2010, 6:08pm UTC](https://www.ruby-forum.com/t/regex-or-line-each-seems-to-not-be-working/182475/1 "2010-01-28T18:08:00Z")

</div>

I have been banging my head over this for 2 days. The following is to  
scan a regular text file line by line using regular expressions. it  
seem to only grab the first line and not iterate through all the lines  
like it should. The “@st == True” is used for Try and Rescue for  
possible failed telnet sessions, and move on to the next.

CODE:

# CE200s and COMMANDS

def dslam\_list()  
@rf.each do |line|  
if line.scan(/^N\w\w\w/)  
@dns = line.to\_s.chomp()  
telnet\_to\_dslam(@dns, @pf)  
if @st == true  
login\_to\_dslam(@un, @pw)  
end  
end

```
  if line.scan(/^get.+/)
    @command = line.to_s.chomp()
    give_commands(@command)
  end

  if line.scan(/^$/)
    sleep (1)
    exit_commands()
  end
end

# close files after finsished with io
@rf.close()
@of.close()

```

end

TEXT FILE:  
The first line is the Host name that is used by telnet method. Telnet  
works fine.

N2CA  
get cmsystem  
get udp  
get system

N7CA  
get cmsystem  
get udp  
get system

---

<div class="post-metadata">

**Author:** ![oshybrid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c68b51/32.png) [@oshybrid](https://www.ruby-forum.com/u/oshybrid)\
**Post date:** [January 28, 2010, 6:55pm UTC](https://www.ruby-forum.com/t/regex-or-line-each-seems-to-not-be-working/182475/2 "2010-01-28T18:55:26Z")

</div>

> ```
> @rf.each do |line|
> 
> ```

does rf have multiple lines in it?  
(you can add a puts line in there, to see).  
-r

---

<div class="post-metadata">

**Author:** ![oshybrid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c68b51/32.png) [@oshybrid](https://www.ruby-forum.com/u/oshybrid)\
**Post date:** [January 28, 2010, 7:00pm UTC](https://www.ruby-forum.com/t/regex-or-line-each-seems-to-not-be-working/182475/3 "2010-01-28T19:00:12Z")

</div>

Roger P. wrote:

> > ```
> > @rf.each do |line|
> > 
> > ```
> 
> does rf have multiple lines in it?  
> (you can add a puts line in there, to see).  
> -r

Yes… @rf is actually the instance variable of the text file listed at  
the end of my original posting

Instantiated as such:

@rf = File.open("#{Dir.pwd}/Script\_Files/BlahBlahBlah.txt, “r”)

---

<div class="post-metadata">

**Author:** ![oshybrid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c68b51/32.png) [@oshybrid](https://www.ruby-forum.com/u/oshybrid)\
**Post date:** [January 28, 2010, 7:12pm UTC](https://www.ruby-forum.com/t/regex-or-line-each-seems-to-not-be-working/182475/4 "2010-01-28T19:12:42Z")

</div>

Brad Mr wrote:

> if line.scan(/^N\w\w\w/)  
> String#scan never returns false. I suppose you want #match:

ruby -v: ruby 1.9.1p378 (2010-01-10 revision 26273) [x86\_64-linux]  
irb(main):001:0\> “foo”.scan(/^xxx/)  
=\> []  
irb(main):002:0\> “foo”.match(/^xxx/)  
=\> nil  
irb(main):003:0\>

Marvin

---

<div class="post-metadata">

**Author:** ![oshybrid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c68b51/32.png) [@oshybrid](https://www.ruby-forum.com/u/oshybrid)\
**Post date:** [January 28, 2010, 8:45pm UTC](https://www.ruby-forum.com/t/regex-or-line-each-seems-to-not-be-working/182475/5 "2010-01-28T20:45:48Z")

</div>

> Perfect… Thanx man that did the trick… Changed everything to  
> “match” instead of “scan”… Keep a mental not on that.

Believe it or not, I run into that one _all the time_

Perhaps there should be an RCR for scan to return nil on no match?

-r

---

<div class="post-metadata">

**Author:** ![oshybrid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c68b51/32.png) [@oshybrid](https://www.ruby-forum.com/u/oshybrid)\
**Post date:** [January 28, 2010, 10:47pm UTC](https://www.ruby-forum.com/t/regex-or-line-each-seems-to-not-be-working/182475/6 "2010-01-28T22:47:30Z")

</div>

On 01/28/2010 08:45 PM, Roger P. wrote:

> > Perfect… Thanx man that did the trick… Changed everything to  
> > “match” instead of “scan”… Keep a mental not on that.
> 
> Believe it or not, I run into that one _all the time_
> 
> Perhaps there should be an RCR for scan to return nil on no match?

IMHO that would be inconsistent. Think about how SQL does it: if you  
query for one element, you get a result set with one element which can  
be NULL or a value != NULL. If you query for multiple elements you get  
a potentially empty result set. I believe the number of APIs that  
return collections and which return nil, null or NULL in case nothing is  
found is a minority.

Btw, I usually use #scan with the block. In that case I don’t really  
care about the return value of #scan. 🙂

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![oshybrid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c68b51/32.png) [@oshybrid](https://www.ruby-forum.com/u/oshybrid)\
**Post date:** [January 28, 2010, 7:29pm UTC](https://www.ruby-forum.com/t/regex-or-line-each-seems-to-not-be-working/182475/7 "2010-01-28T19:29:56Z")

</div>

Marvin GÃ¼lker wrote:

> Brad Mr wrote:
> 
> > if line.scan(/^N\w\w\w/)  
> > String#scan never returns false. I suppose you want #match:
> 
> ruby -v: ruby 1.9.1p378 (2010-01-10 revision 26273) [x86\_64-linux]  
> irb(main):001:0\> “foo”.scan(/^xxx/)  
> =\> []  
> irb(main):002:0\> “foo”.match(/^xxx/)  
> =\> nil  
> irb(main):003:0\>
> 
> Marvin

Perfect… Thanx man that did the trick… Changed everything to  
“match” instead of “scan”… Keep a mental not on that.
