my $str = "|90|93|foo|bar|91|92|95|96|906|"; $str =~ s/(?<=\|)9[0-6](?=\|)/X/g; print "[ $str ]\n"; __END__ Also , you can go through this tutorial