#!/usr/bin/perl -w use strict; my $str = "|90|93|foo|bar|91|92|95|96|906|"; $str =~ s/9([0-9]+)?/x/g; print "$str \n";