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

did I get greedy quantifiers wrong ?

Thread Next
From:
Sharan Basappa
Date:
May 31, 2007 03:02
Subject:
did I get greedy quantifiers wrong ?
Message ID:
8a31202a0705310302y1822ca58y9507522e3b9a262d@mail.gmail.com
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
?

Thanks,
Sharan

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About