J
Jeremy Henty
I'm stumped: why do the regular expressions Z? , (?=Z)? and ((?=Z))?
*not* match the same things?
string = 'a'
[ %r{Z?},
%r{(?=Z)?},
%r{((?=Z))?},
].each do |re|
puts re.match(string) ? "yes" : "no"
end
Result (ruby 1.8.4 on Linux):
yes
no [!!!]
yes
I thought that all three should look for a 'Z', but successfully match
nothing if there is no 'Z'. Why does adding lookahead in the second
make the match fail? Surely lookahead only changes whether the match
consumes anything? It shouldn't change whether there is a match or
not. And why does adding parentheses in the third make the match
succeed again?
Regards,
Jeremy Henty
*not* match the same things?
string = 'a'
[ %r{Z?},
%r{(?=Z)?},
%r{((?=Z))?},
].each do |re|
puts re.match(string) ? "yes" : "no"
end
Result (ruby 1.8.4 on Linux):
yes
no [!!!]
yes
I thought that all three should look for a 'Z', but successfully match
nothing if there is no 'Z'. Why does adding lookahead in the second
make the match fail? Surely lookahead only changes whether the match
consumes anything? It shouldn't change whether there is a match or
not. And why does adding parentheses in the third make the match
succeed again?
Regards,
Jeremy Henty