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?
Mike
···
–
mike@stok.co.uk | The “`Stok’ disclaimers” apply.
http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60
http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA