# File.open with line =~ /.../

**URL:** <https://rubytalk.org/t/file-open-with-line/35610>\
**Category:** ruby-talk\
**Created:** [23 February 2007 07:36 UTC](https://rubytalk.org/t/file-open-with-line/35610 "2007-02-23T07:36:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rebhan\_Gilbert](https://avatars.discourse-cdn.com/v4/letter/r/c5a1d2/32.png) [@Rebhan\_Gilbert](https://rubytalk.org/u/Rebhan_Gilbert)\
**Post date:** [23 February 2007 07:36 UTC](https://rubytalk.org/t/file-open-with-line/35610/1 "2007-02-23T07:36:05Z")

</div>

Hi,

i have a txtfile like that =

bla1  
foo=red

bla2  
foo=green

bla3  
foo=yellow

and i want to get i.e. bla1 and the corresponding value of foo

i tried with

File.open("Y:/test/sample.txt", "r").each do |line|  
&nbsp;&nbsp;puts $1\<\<' : '\<\< $3 if line =~ /(\w+)\s+(\w+=)(\w+)/  
end

but somehow the regex doesn't work ?!  
whereas it works in the QuickRex Plugin for Eclipse

Any ideas ?

Regards, Gilbert

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [23 February 2007 08:15 UTC](https://rubytalk.org/t/file-open-with-line/35610/2 "2007-02-23T08:15:08Z")

</div>

If this is really the case I'd say the QuickRex Plugin is broken or you feed different files. Since you match for a non zero number of white space characters before the equal sign, it should not match, since there is none in the file contents you showed. You could change to \s\* or leave it out completely.

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 23.02.2007 08:36, Rebhan, Gilbert wrote:
> 
> > Hi,
> > 
> > i have a txtfile like that =
> > 
> > bla1  
> > foo=red
> > 
> > bla2  
> > foo=green
> > 
> > bla3  
> > foo=yellow
> > 
> > and i want to get i.e. bla1 and the corresponding value of foo
> > 
> > i tried with
> > 
> > File.open("Y:/test/sample.txt", "r").each do |line|  
> > &nbsp;&nbsp;puts $1\<\<' : '\<\< $3 if line =~ /(\w+)\s+(\w+=)(\w+)/  
> > end
> > 
> > but somehow the regex doesn't work ?!  
> > whereas it works in the QuickRex Plugin for Eclipse
> > 
> > Any ideas ?

---

<div class="post-metadata">

**Author:** ![Carlos](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@Carlos](https://rubytalk.org/u/Carlos)\
**Post date:** [23 February 2007 09:45 UTC](https://rubytalk.org/t/file-open-with-line/35610/3 "2007-02-23T09:45:57Z")

</div>

Rebhan, Gilbert wrote:

> Hi,
> 
> i have a txtfile like that =
> 
> bla1  
> foo=red
> 
> bla2  
> foo=green
> 
> bla3  
> foo=yellow
> 
> and i want to get i.e. bla1 and the corresponding value of foo
> 
> i tried with
> 
> File.open("Y:/test/sample.txt", "r").each do |line|  
> &nbsp;&nbsp;puts $1\<\<' : '\<\< $3 if line =~ /(\w+)\s+(\w+=)(\w+)/  
> end
> 
> but somehow the regex doesn't work ?!  
> whereas it works in the QuickRex Plugin for Eclipse

Well... for each line of the file you are trying to match 2 lines. That's impossible :). Read the file in "paragraph mode" and match each paragraph, or read it whole and scan it for your regexp.

For example (not tested):

File.read('...').scan(/^(\w+)\s\*\n\w+=(\w+)$/) {|a,b| puts a+": "+b}

Good luck.

> **···**
>
> --

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [23 February 2007 11:01 UTC](https://rubytalk.org/t/file-open-with-line/35610/4 "2007-02-23T11:01:03Z")

</div>

It's because you're only searching within each line individually, whereas  
your regexp matches ( word, space, word=word ) in one go. Using 'each' it  
will be invoked first with "bla1\n" and then with "foo=red\n", neither of  
which matches by itself.

If it's a small file then just slurp it in all in one go:

ans = File.open("sample.txt").read.scan(/(\w+)\s+(\w+=)(\w+)/)  
p ans

Your regexp works, but personally I would add some anchors, i.e.

ans = File.open("sample.txt").read.scan(/^(\w+)\s+(\w+=)(\w+)$/)  
p ans

If you want to avoid reading the whole file in, there are more complex  
solutions, e.g.

require 'enumerator'  
ans = File.open("sample.txt").each\_cons(2) do |line1, line2|  
&nbsp;&nbsp;next unless line1 =~ /^(\w+)$/  
&nbsp;&nbsp;cat = $1  
&nbsp;&nbsp;next unless line2 =~ /^(\w+)=(\w+)$/  
&nbsp;&nbsp;puts "#{cat}: #{$1} =\> #{$2}"  
end

HTH,

Brian.

> **···**
>
> On Fri, Feb 23, 2007 at 04:36:05PM +0900, Rebhan, Gilbert wrote:
> 
> > Hi,
> > 
> > i have a txtfile like that =
> > 
> > bla1  
> > foo=red
> > 
> > bla2  
> > foo=green
> > 
> > bla3  
> > foo=yellow
> > 
> > and i want to get i.e. bla1 and the corresponding value of foo
> > 
> > i tried with
> > 
> > File.open("Y:/test/sample.txt", "r").each do |line|  
> > &nbsp;&nbsp;puts $1\<\<' : '\<\< $3 if line =~ /(\w+)\s+(\w+=)(\w+)/  
> > end
> > 
> > but somehow the regex doesn't work ?!  
> > whereas it works in the QuickRex Plugin for Eclipse
> > 
> > Any ideas ?

---

<div class="post-metadata">

**Author:** ![Rebhan\_Gilbert](https://avatars.discourse-cdn.com/v4/letter/r/c5a1d2/32.png) [@Rebhan\_Gilbert](https://rubytalk.org/u/Rebhan_Gilbert)\
**Post date:** [23 February 2007 08:59 UTC](https://rubytalk.org/t/file-open-with-line/35610/5 "2007-02-23T08:59:55Z")

</div>

Hi,

> **···**
>
> -----Original Message-----  
> From: Robert Klemme [mailto:shortcutter@googlemail.com]  
> Sent: Friday, February 23, 2007 9:15 AM  
> To: ruby-talk ML  
> Subject: Re: File.open with line =~ /.../
> 
> On 23.02.2007 08:36, Rebhan, Gilbert wrote:
> 
> > Hi,
> > 
> > i have a txtfile like that =
> > 
> > bla1  
> > foo=red
> > 
> > bla2  
> > foo=green
> > 
> > bla3  
> > foo=yellow
> > 
> > and i want to get i.e. bla1 and the corresponding value of foo
> > 
> > i tried with
> > 
> > File.open("Y:/test/sample.txt", "r").each do |line|  
> > &nbsp;&nbsp;puts $1\<\<' : '\<\< $3 if line =~ /(\w+)\s+(\w+=)(\w+)/  
> > end
> > 
> > but somehow the regex doesn't work ?!  
> > whereas it works in the QuickRex Plugin for Eclipse
> > 
> > Any ideas ?
> 
> /\*  
> If this is really the case I'd say the QuickRex Plugin is broken or you  
> feed different files. Since you match for a non zero number of white  
> space characters before the equal sign, it should not match, since there
> 
> is none in the file contents you showed. You could change to \s\* or  
> leave it out completely.  
> \*/
> 
> hmm, the QuickRex Plugin can evaluate with the Regular Expression  
> Implementation for =
> 
> Java  
> Jakarta Oro Perl 5  
> Jakarta Oro Awk  
> JRegex
> 
> the regex  
> (\w+)\r\n(\w+=)(\w+) works in all implementations except the Awk  
> also the regex (\w+)\s+(\w+=)(\w+) works for all except the Awk  
> implementation
> 
> so i get = $1 \> bla1 $2 \>foo $3 \> red with both regex
> 
> Did only simple regex operations on one line in ruby until now.  
> What's wrong with my regex ?
> 
> Regards, Gilbert

---

<div class="post-metadata">

**Author:** ![Rebhan\_Gilbert](https://avatars.discourse-cdn.com/v4/letter/r/c5a1d2/32.png) [@Rebhan\_Gilbert](https://rubytalk.org/u/Rebhan_Gilbert)\
**Post date:** [23 February 2007 10:20 UTC](https://rubytalk.org/t/file-open-with-line/35610/6 "2007-02-23T10:20:52Z")

</div>

Hi,

> **···**
>
> -----Original Message-----  
> From: Carlos [mailto:angus@quovadis.com.ar]  
> Sent: Friday, February 23, 2007 10:46 AM  
> To: ruby-talk ML  
> Subject: Re: File.open with line =~ /.../
> 
> > i tried with
> > 
> > File.open("Y:/test/sample.txt", "r").each do |line|  
> > &nbsp;&nbsp;puts $1\<\<' : '\<\< $3 if line =~ /(\w+)\s+(\w+=)(\w+)/  
> > end
> > 
> > but somehow the regex doesn't work ?!  
> > whereas it works in the QuickRex Plugin for Eclipse
> 
> /\*  
> Well... for each line of the file you are trying to match 2 lines.  
> That's impossible :). Read the file in "paragraph mode" and match each  
> paragraph, or read it whole and scan it for your regexp.  
> \*
> 
> Strike, ouch 😉  
> yup line != lines
> 
> /\*  
> For example (not tested):
> 
> File.read('...').scan(/^(\w+)\s\*\n\w+=(\w+)$/) {|a,b| puts a+": "+b}  
> \*/
> 
> works like a charm. Thanks !!
> 
> Regards, Gilbert

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [23 February 2007 13:40 UTC](https://rubytalk.org/t/file-open-with-line/35610/7 "2007-02-23T13:40:09Z")

</div>

Maybe I was too fast - I overlooked that you are trying to match multiple lines. In that case of course you have to feed multiple lines - not a single line at a time as Carlos pointed out.

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 23.02.2007 09:59, Rebhan, Gilbert wrote:
> 
> > Hi,
> > 
> > -----Original Message-----  
> > From: Robert Klemme [mailto:shortcutter@googlemail.com] Sent: Friday, February 23, 2007 9:15 AM  
> > To: ruby-talk ML  
> > Subject: Re: File.open with line =~ /.../
> > 
> > On 23.02.2007 08:36, Rebhan, Gilbert wrote:
> > 
> > > Hi,
> > > 
> > > i have a txtfile like that =
> > > 
> > > bla1  
> > > foo=red
> > > 
> > > bla2  
> > > foo=green
> > > 
> > > bla3  
> > > foo=yellow
> > > 
> > > and i want to get i.e. bla1 and the corresponding value of foo
> > > 
> > > i tried with
> > > 
> > > File.open("Y:/test/sample.txt", "r").each do |line|  
> > > &nbsp;&nbsp;puts $1\<\<' : '\<\< $3 if line =~ /(\w+)\s+(\w+=)(\w+)/  
> > > end
> > > 
> > > but somehow the regex doesn't work ?!  
> > > whereas it works in the QuickRex Plugin for Eclipse
> > > 
> > > Any ideas ?
> > 
> > /\*  
> > If this is really the case I'd say the QuickRex Plugin is broken or you feed different files. Since you match for a non zero number of white space characters before the equal sign, it should not match, since there
> > 
> > is none in the file contents you showed. You could change to \s\* or leave it out completely.  
> > \*/
> > 
> > hmm, the QuickRex Plugin can evaluate with the Regular Expression  
> > Implementation for =
> > 
> > Java  
> > Jakarta Oro Perl 5  
> > Jakarta Oro Awk  
> > JRegex
> > 
> > the regex  
> > (\w+)\r\n(\w+=)(\w+) works in all implementations except the Awk  
> > also the regex (\w+)\s+(\w+=)(\w+) works for all except the Awk  
> > implementation
> > 
> > so i get = $1 \> bla1 $2 \>foo $3 \> red with both regex
> > 
> > Did only simple regex operations on one line in ruby until now.  
> > What's wrong with my regex ?
