my $nth = 3; my $rep = 1; my $chr = 'e'; my $str = "Terence and Philip are sweet"; $str =~ m/$chr/gc for (1..$nth); # eat up first $nth "$chr"s $str =~ s/\G(.*?)$chr/$1$rep/g; # replace the rest print $str;