in reply to Re: Interesting Regex Behavior
in thread Interesting Regex Behavior
In the first example, the quantifiers aren't lazy, but greedy, and THAT is why $2 is undef and $1 holds the empty string. First, the (a)* matches the "a", and stores "a" in $2, and so $1 is "a" as well. Then the outermost * makes the capturing block try again, and this time (a)* matches ZERO "a"s. Here's the trick: "y" =~ /(x)?/ stores undef in $1, and it succeeds. Therefore, $2 becomes undef, and $1 becomes the empty string.
The second regex works thus. First, the (a) matches the "a" ($1 is "a", $2 is "a", and $3 is undef). Then the (b) matches the "b", but it does NOT reset $2's value to undef, even though (a) didn't match. Therefore, $1 is "b" (note: "japhy" =~ /(\w)+/ stores "y" in $1), $2 is "a", and $3 is "b".
_____________________________________________________
Jeff[japhy]Pinyan:
Perl,
regex,
and perl
hacker, who'd like a job (NYC-area)
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;
|
|---|