Brian Candler
2/23/2007 11:01:00 AM
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|
> 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 ?
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|
next unless line1 =~ /^(\w+)$/
cat = $1
next unless line2 =~ /^(\w+)=(\w+)$/
puts "#{cat}: #{$1} => #{$2}"
end
HTH,
Brian.