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