You'll probably want to escape metacharacters either using quotemeta or wrapping your terms with \Q and \E.
You may also wish to wrap your terms with \b to indicate word boundaries.
I note that you have a term that contains whitespace. Are you confident that all your whitespace will be single spaces, as opposed to tabs or newlines?
One feature of LanX's solution that was not mentioned was that because the substitution consumes the string, joining with the alternator prevents double substitution.
This will not fix the whitespace issue. To do that, you'd probably need to slurp the file, modify the regex to treat whitespace agnostically, and then replicate the type of whitespace in your result. Doing this is probably a giant pain.my $re = join '|', map "\Q$_\E", sort {length $b <=> length $a} keys % +dictionary; while (<FILEIN>) { s/($re)/$dictionary{$1}/g }
#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.
In reply to Re: replace text using hash
by kennethk
in thread replace text using hash
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |