I could be horribly wrong ;-( would not be the first time.
I think you have read it backward
$string = "||was not was not was not the time|| was not was not was not
was not ";
Your second expression greps ||was not was not was not the time||
Your first expression is the greedy one it grep to last was of the 2nd
expression ||was not was not was||
So if you rewrite the expression
my $string = "was not was not was not was not was not was not the time
was not first was not second was not third was not forth ";
if($string =~ m{(.*)was(.*) was(.*) was (.*) }) {
print "MTC $string\n";
print "1 is '$1'\n";
print "2 is '$2'\n";
print "3 is '$3'\n";
print "4 is '$4'\n";
}
C:\>perl t1.pl
MTC was not was not was not was not was not was not the time was not
first was not second was not third was not forth
1 is 'was not was not was not was not was not was not the time was not
first '
2 is ' not second'
3 is ' not third'
4 is 'not forth'
Just read it Backward.
Best of luck
Best Regards
Afshin Bozorgzadeh
When you seek it, you cannot find it.
Your hand cannot reach it Nor your mind exceed it.
When you no longer seek it,It is always with you.
-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Greg London
Sent: Wednesday, February 01, 2006 2:55 PM
To: [email protected]
Subject: [Boston.pm] quantifiers in regular expressions
I'm trying to understand how multiple quantifiers in a
regular expressions get massaged to fit the data.
in other words.
my $string = "was not was not was not the time was not was not was not
was not ";
if($string =~ m{(.*)was(.*)the time}) {
print "matched\n";
print "1 is '$1'\n";
print "2 is '$2'\n";
}
The first (.*) swallows the whole string,
and fails when it sees "was" in the regular
expression, so it backs up until it can find
a "was" in the string, and then it fails when
it looks for "the time", so it backs up again
until it finds a "was" followed by "the time".
I know it "just works", but I'm trying to figure
out exactly how it behaves under the hood.
I think if I were to write it in pseudocode,
it would look something like this:
foreach my $quantifier ( @list_of_quantifiers ) { # ($1 .. $n)
while(nomatch) {
$quantifier->reducecaptureby1andtryagain;
}
But I think there might be other ways of
implementing it that would be subtly diffferent.
anyone?
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm