From: Robert Klemme Date: 2007-02-23T22:40:09+09:00 Subject: Re: File.open with line =~ /.../ 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| >> 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 ? 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 robert