my $nth = 3; my $rep = 1; my $chr = 'e'; my $i = 1; $_ = "Terence and Philip are sweet"; s/($chr)/$i++<$nth?$1:$rep/eg; print;