in reply to how to search and replace the match with specific number of times in a string
Or, in the rare case searching from the beginning over and over again is expensive, or if a replace could create a new match:$string =~ s/a/i/ for 1 .. $N;
my $count = 0; $string =~ s/a(??{++ $count <= $N ? "(*ACCEPT)" : "(*FAIL)"})/i/g; # Use '""' instead of '(*ACCEPT)' and '(?!)' instead of '(*FAIL)' on o +ld Perls.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: how to search and replace the match with specific number of times in a string
by AnomalousMonk (Archbishop) on Jan 29, 2010 at 18:00 UTC |