$string = "We modify foos and bars but not foobars.\n"; my %trans = qw( foo bar bar foo foobar foobar ); # reverse sort is a hack to put strings before # substrings. (ie foobar before foo) my $re_str = join "|", map quotemeta($_), reverse sort keys %trans; $string =~ s/($re_str)/$trans{$1}/g; print $string;