my $regex = join "|", map quotemeta, keys %substitute_hash; $regex = qr/$regex/; # compile ... $string =~ s/($regex)/$substitute_hash{$1}/g;