my $str = "blublublublublu"; my $such='lu'; my $repl='LA'; my $count = 3; my $pos=0; while($count and $pos >= 0) { $pos=index($str,$such,$pos); $count--; } if($pos>0){ substr($str,$pos,length($such),$repl); }