in reply to Re: Non-greedy regex behaves greedily
in thread Non-greedy regex behaves greedily
Your regex doesn't force a non-greedy behaviour.
I'll try to explain with a simplified text example:
my $text = <<TEXT; 000ABCDEFABCGHI TEXT if ( $text =~ m{(ABC.*?)$} ) { print $1, $/; }
The engine reads $text from left to right and will have a try with starting at the first "ABC", using the complete following string until end of line. As that's exactly what the regex requested, this result is returned. There's no condition which forces the engine to search for a shorter result. There will be no second run which checks, if the current result may contain a shorter result.
The first valid match will be returned; this isn't always the best match.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Non-greedy regex behaves greedily
by kovacsbv (Novice) on Jul 27, 2008 at 23:36 UTC | |
by ysth (Canon) on Jul 28, 2008 at 10:03 UTC |