Perl-Sensitive Sunglasses | |
PerlMonks |
How Greedy is Greedy: A Regex Questionby dsb (Chaplain) |
on Jul 03, 2001 at 22:26 UTC ( [id://93618]=perlquestion: print w/replies, xml ) | Need Help?? |
dsb has asked for the wisdom of the Perl Monks concerning the following question:
I've read in several places that the '.*' construct is not the most efficient way to grab multiple characters because of its greediness, or its first directive to match as much as possible, only matching less when there is a sub-expression further in the regex that is required to match and can't.
That I understand. My question is this:
For example:
And say(for arguments sake, I would not use this regular expression if not for the sake of this question) that the regex used was: So I wondered, would the '.*' construct in this case match only the text from the first link tag, or would it match everything from the end of the first URL up to the closing tag of the second link? So that $2 would contain: Perlmonks</a> is a great site, so is <a href="http://www.devshed.com">DevShed The answer is yes(I tried it). But when the '.*?' construct is used, only 'Perlmonks' is matched. So the question mark apparently makes the '.*' construct non-greedy. So is it the optional nature of '?' that makes the '.*' construct non-greedy, or is it something else entirely? Amel - f.k.a. - kel
Back to
Seekers of Perl Wisdom
|
|