while ($string =~ /abc/ig) { my $ins = sprintf(" def(%s)", $x++); my $pos = $+[0]; $string =~ s/\G/$ins/; pos($string) = $pos + length($ins); }