D
dblack
Hi --
For some reason, lookbehind and alternation seem not to be playing
together in a little Oniguruma test. This is based on the string
splitting thread from a little while ago this evening, and uses a CVS
1.9.0 Ruby acquired about 1/2 an hour ago.
str = %Q{abc def "ghi jkl" mno}
# Look for "..." but just get the ... part:
re1 = /(?<=")[^"]+(?=")/
# Test that:
p str.scan(re1) # => ["ghi jkl"]
# Now, do the same thing *or* \S+. This should, I think,
# pick up the abc, def, and mno substrings too.
re2 = /((?<=")[^"]+(?="))|(\S+)/
# But it doesn't; the part before the alternation never
# matches, even though it did before (as shown by the
# captures):
p str.scan(re2)
# => [[nil, "abc"], [nil, "def"], [nil, "\"ghi"], [nil, "jkl\""],
# [nil, "mno"]]
I know that's all a bit cluttered, but the basic thing is that a
sub-pattern using lookbehind doesn't seem to match any more when
there's an alternation. Instead, only the second alternative ever
matches.
Does anyone know why?
David
--
David A. Black
(e-mail address removed)
"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
For some reason, lookbehind and alternation seem not to be playing
together in a little Oniguruma test. This is based on the string
splitting thread from a little while ago this evening, and uses a CVS
1.9.0 Ruby acquired about 1/2 an hour ago.
str = %Q{abc def "ghi jkl" mno}
# Look for "..." but just get the ... part:
re1 = /(?<=")[^"]+(?=")/
# Test that:
p str.scan(re1) # => ["ghi jkl"]
# Now, do the same thing *or* \S+. This should, I think,
# pick up the abc, def, and mno substrings too.
re2 = /((?<=")[^"]+(?="))|(\S+)/
# But it doesn't; the part before the alternation never
# matches, even though it did before (as shown by the
# captures):
p str.scan(re2)
# => [[nil, "abc"], [nil, "def"], [nil, "\"ghi"], [nil, "jkl\""],
# [nil, "mno"]]
I know that's all a bit cluttered, but the basic thing is that a
sub-pattern using lookbehind doesn't seem to match any more when
there's an alternation. Instead, only the second alternative ever
matches.
Does anyone know why?
David
--
David A. Black
(e-mail address removed)
"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black