my $s = "Terence and Philip are sweet\n"; my $c = 'e'; my $r = 1; my $n = 3; ($s=~/((.*?)$c){$n}/)&&(($s,$_)=($&,$'))&&(s/$c/$r/g)&&(print $s,$_);