in reply to pattern matching a limited number of times
Now the current (perl5) syntax for a regex assertion is very awkward. This will do it:
NOTCOND is perl code producing a true when you want the assertion to fail, because only in that case an attempt to match the regex snippet /(?!)/ is made, and this is a lookahead that never matches./(?(?{NOTCOND})(?!))/
So, you can try this:
my $i = 0; s/ab(?(?{++$i>5})(?!))/cd/g;
This will indeed stop matching after the fifth match, producing the desired result; but it will not stop trying: it will go through the whole string, and if your string contains 50 substrings "ab", then the assertion code will be called 50 times. So for long strings, it won't be too efficient.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re2: pattern matching a limited number of times
by blakem (Monsignor) on Sep 14, 2002 at 09:42 UTC | |
by bart (Canon) on Sep 14, 2002 at 10:14 UTC | |
by BrowserUk (Patriarch) on Sep 14, 2002 at 10:48 UTC |