my $nth = 3; my $rep = 1; my $chr = 'e'; my $str = "Terence and Philip are sweet\n"; my $pos = 0; $pos = index($str, $chr, $pos+1) for(1..$nth); substr($str, $pos) =~ s/$chr/$rep/g; print $str;