develooper Front page | perl.beginners | Postings from May 2007

did I get greedy quantifiers wrong ?

Thread Next
Sharan Basappa
May 31, 2007 03:02
did I get greedy quantifiers wrong ?
Message ID:
I seem to be having some conceptual problem with greedy quantifiers ..
My understanding is that it matches as much as follows while still
allowing rest of the
regex to match.
But look at the following example :
$str = mississippi;
$str =~ m/m(.*i)(.*pi)/;
print "one is $1 \n";
print "two is $2 \n";

$str = mississippi;
$str =~ m/m(.*i?)(.*pi)/;
print "one is $1 \n";
print "two is $2 \n";

In the first code snippet, I expected first regex (.*i) to match till
ississip and leave pi for (.*pi) regex.

But what I get as the output of this script is :

one is ississi
two is ppi
one is ississip
two is pi

Why is that perl is leaving ppi to second regex while it can continue
till first p in ppi and can still get the second regex to get a match


Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About