dsb has asked for the wisdom of the Perl Monks concerning the following question:
That I understand. My question is this:
If there are two of a similar pattern in a string will the '.*' match both of them if the terminating sequence is the same?
For example:
Say the string to matched against is:
<a href="http://www.perlmonks.org">Perlmonks</a> is a great site, so is <a href="http://www.devshed.com">DevShed</a>
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:$string =~ m%<a href="([^"]+)">(.*)</a>%;
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How Greedy is Greedy: A Regex Question
by tachyon (Chancellor) on Jul 03, 2001 at 22:33 UTC | |
|
Re: How Greedy is Greedy: A Regex Question
by japhy (Canon) on Jul 04, 2001 at 01:42 UTC | |
|
Re: How Greedy is Greedy: A Regex Question
by particle (Vicar) on Jul 03, 2001 at 22:39 UTC | |
|
Re: How Greedy is Greedy: A Regex Question
by clemburg (Curate) on Jul 04, 2001 at 12:23 UTC | |
|
Re: How Greedy is Greedy: A Regex Question
by Spenser (Friar) on Mar 18, 2007 at 23:22 UTC | |
by dsb (Chaplain) on Apr 25, 2007 at 14:00 UTC |