>perl -wMstrict -le "my %replace = ( '111' => 'bar', '1111' => 'world', '333' => 'perl', ); my $find = join '|', reverse sort keys %replace; ;; my $str = 'f111,f1111,f333'; $str =~ s{ f ($find) }{$replace{$1}}xmsg; print qq{'$str'}; " 'bar,world,perl'