in reply to Search for <n>th occurrence of regex
# Appends to the 3rd occurence of 'abc' from start-of-line ($a = $ref) =~ s/^(?:.*?abc\K){3}/___/; print $a; # Appends to the 4th occurence of 'abc' from start-of-line ($a = $ref) =~ s/^(?:.*?abc\K){4}/___/; print $a; # omit ^ to append to every 2nd occurence of 'abc' ($a = $ref) =~ s/(?:.*?abc\K){2}/___/g; print $a; # omit ^ to append to every 1st occurence of 'abc' ($a = $ref) =~ s/(?:.*?abc\K){1}/___/g; print $a;
Of course, the "{1}" is optional in the last pattern.
|
|---|