On Jul 29, Pine Yan said:
line3: print "\$1 = $1 [EMAIL PROTECTED],$+[0]}, \$& = $&\n"
if($string3
=~ /(a|b)*/);
line4: print "\$1 = $1 [EMAIL PROTECTED],$+[0]}, \$& = $&\n"
if($string4
=~ //);
$1 = a @{0,2}, $& = ba
$1 = @{0,0}, $& =
I'll go over it again. $string3 starts with "bac...", and $string4
starts with "xyx...".
When the regex /(a|b)*/ is applied to "bac...", this is what it does:
# 'a' or 'b', zero or more times
b a c ......
^ position 0: a? no
^ position 0: b? yes
b a c ......
^ position 1: a? yes
b a c ......
^ position 2: a? no
^ position 2: b? no
# successful match from position 0 to position 2 ("ba")
When the regex /(a|b)*/ is applied to "xyx...", this is what it does:
# 'a' or 'b', zero or more times
x y x ......
^ position 0: a? no
^ position 0: b? no
# successful match from position 0 to position 0 ("")
This is because the * quantifier means that ZERO matches of that token is
a perfectly acceptable outcome. The regex got a match at the left-most
position it tried, so it uses that match.
LEFT-MOST, and from there, LONGEST.
--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>