my %replace = ( m => "MPZ", a => "APZ", ... ); my $re = join '|', keys %replace; $string =~ s/\b($re)\b/$replace{$1}/g;