# 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;