my $match = "\Q" . join ("\E|\Q", keys %translate) . "\E"; #### >perl -wMstrict -le "my %translate = ( 'tr??u++e' => 1, 'fa+l?se' => 0, ); ;; my $match = qq{\Q} . join(qq{\E|\Q}, keys %translate) . qq{\E}; print qq{'$match'}; $match = qr{$match}; print $match; ;; my $mooch = '\Q' . join('\E|\Q', keys %translate) . '\E'; print qq{'$mooch'}; $mooch = qr{$mooch}; print $mooch; " 'tr??u++e|fa+l?se' (?-xism:tr??u++e|fa+l?se) '\Qtr??u++e\E|\Qfa+l?se\E' Unrecognized escape \Q passed through in regex... Unrecognized escape \E passed through in regex... Unrecognized escape \Q passed through in regex... Unrecognized escape \E passed through in regex... (?-xism:\Qtr??u++e\E|\Qfa+l?se\E) #### >perl -wMstrict -le "my %xlate = ( A => '1', AA => '2', AAA => '3', AAAA => '4', ); my $mooch = join '|', keys %xlate; $mooch = qr{ (?i) $mooch }xms; my $match = join '|', reverse sort keys %xlate; $match = qr{ (?i) $match }xms; print $mooch; print $match; ;; my $s = 'xAxAAxAAAxAAAAx'; print qq{'$s'}; $s =~ s{ ($mooch) }{$xlate{ uc $1 }}xmsg; print qq{'$s'}; ;; $s = 'xAxaaxAaAxaAaAx'; print qq{'$s'}; $s =~ s{ ($match) }{$xlate{ uc $1 }}xmsg; print qq{'$s'}; " (?msx-i: (?i) A|AA|AAAA|AAA ) (?msx-i: (?i) AAAA|AAA|AA|A ) 'xAxAAxAAAxAAAAx' 'x1x11x111x1111x' 'xAxaaxAaAxaAaAx' 'x1x2x3x4x' #### >perl -wMstrict -le "my %xlate = ( 'string a' => '3', 'string b' => '2', true => '1', false => '0', tt => '22', ttt => '33', ); my $xl = join '|', map { quotemeta } reverse sort keys %xlate; $xl = qr{ (?i) \b (?: $xl) \b }xms; ;; my $s = 'vv TTT ww TT String A xx sTrInG b xtruex True yy FALSE zz'; print qq{'$s'}; ;; $s =~ s{ ($xl) }{$xlate{ lc $1 }}xmsg; print qq{'$s'}; " 'vv TTT ww TT String A xx sTrInG b xtruex True yy FALSE zz' 'vv 33 ww 22 3 xx 2 xtruex 1 yy 0 zz'