[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Regular expression: Is this a bug or feature?

Martin Kahlert

1/23/2007 8:17:00 AM

Hi ruby experts!

Is this intended behaviour?

irb(main):001:0> s1='a=1'
=> "a=1"
irb(main):002:0> s2='b=1'
=> "b=1"
irb(main):003:0> s1 =~ /a|b=(.)/
=> 0 <------ expression matches
irb(main):004:0> $1
=> nil <------ but where is argument?
irb(main):005:0> s2 =~ /a|b=(.)/
=> 0 <------ expression matches
irb(main):006:0> $1
=> "1" <------ this has been expected
irb(main):007:0> s1 =~ /(a|b)=(.)/
=> 0 <------ expression matches
irb(main):012:0> $2
=> "1" <------ this has been expected

Tested on ruby 1.8.2 (2004-12-22) [i686-linux]

Thanks for your help in advance
Martin.

1 Answer

Sylvain Joyeux

1/23/2007 8:41:00 AM

0

> irb(main):001:0> s1='a=1'
> => "a=1"
> irb(main):003:0> s1 =~ /a|b=(.)/
> => 0 <------ expression matches
> irb(main):004:0> $1
> => nil <------ but where is argument?
You wanted to do s1 =~ /(?:a|b)=(.)/. The | operator has a low precedence
in regexps
--
Sylvain Joyeux