"Mike Stok" <mike / ratdog.stok.co.uk> schrieb im Newsbeitrag
news:4dlGa.75319$G_.36633 / news02.bloor.is.net.cable.rogers.com...
> I have been experimenting with building regular expressions from
> components with ruby from CVS and have noticed some unexpected
> behaviour.
>
> To take a simple case, which works as expected:
>
> [mike@ratdog src]$ irb --simple-prompt
> >> m = /[Mm]ike/
> => /[Mm]ike/
> >> s = /[Ss]tok/
> => /[Ss]tok/
> >> 'mike stok' =~ /^#{m} #{s}$/
> => 0
> >> 'Mike Stok' =~ /^#{m} #{s}$/
> => 0
>
> when I try setting up m and s to be case insensitive then
>
> >> m = /mike/i
> => /mike/i
> >> s= /stok/i
> => /stok/i
> >> 'mike stok' =~ /^#{m} #{s}$/
> => nil
>
> I would have expected that to work, so I look at what the regular
> expression looks like:
>
> => /^(?i-mx:mike) (?i-mx:stok)$/
> >> 'mike stok' =~ /^(?i-mx:mike) (?i-mx:stok)$/
> => nil
> # I expected 0 here, not nil
> >> 'mike stok' =~ /^(?i:mike) (?i:stok)$/
> => 0
>
> Removing the -mx seems to make it work, and as m and x modifiers don't
> matter to this match I try with them:
>
> >> 'Mike Stok' =~ /^(?imx:mike) (?imx:stok)$/
> => 0
>
> Is there a flaw in the handling of the - in (?i-mx: ... ) in the regular
> expression?  In Perl it seems to work, so I don't think my expectation
> is too wild:
>
>   DB<1> print 'mike stok' =~ /^(?i-mx:mike) (?i-mx:stok)$/
>   1
>
>
> It would be good if this worked, as I can then build regular expressions
> out of sub-expressions without interpolating strings...
>
> Is this a flaw in Ruby or my expectations?

I think the problem is, that you use #{m} to insert a regexp into another:

irb(main):006:0> m = /[Mm]ike/
/[Mm]ike/
irb(main):007:0> m.to_s
"(?-mix:[Mm]ike)"
irb(main):008:0> m.source
"[Mm]ike"
irb(main):009:0>

You'd be better off if you use

'mike stok' =~ /^#{m.source} #{s.source}$/

or make m and s strings in the first place.

Cheers

    robert