in reply to changing only the first two matches of a regex
Here the $count variable is counting instances of matches. When it gets past two, the original string is replaced with itself, otherwise the desired change is made. The /e modifier says that the right side is Perl code to be executed rather than simply a double-quoted string.{ my $count = 0; s/(\d+_([a-z]+))/++$count > 2 ? $1 : $2/eg; }
-- Randal L. Schwartz, Perl hacker
|
|---|