NB: If the word | substring translations are essentially one-to-one, you can use the technique discussed in haukex's Building Regex Alternations Dynamically article to do a fairly fast search/replace:
c:\@Work\Perl\monks>perl
use strict;
use warnings;
use Test::More 'no_plan';
use Test::NoWarnings;
my %replace = qw(
milk white toast brown cheese yellow peas green
);
my ($rx_search) =
map qr{ (?i) (?<! [-\w]) (?: $_) (?! [-\w]) }xms,
join ' | ',
map quotemeta,
reverse sort
keys %replace
;
print $rx_search, "\n"; # for debug
VECTOR:
for my $ar_vector (
'no changes in these',
[ 'milky appease peasoup cheese-toast'
=>
'milky appease peasoup cheese-toast'
],
'parts of all these should change',
[ 'mIlK, some PeAs, cheese and toast.'
=>
'white, some green, yellow and brown.'
],
) {
if (not ref $ar_vector) {
note $ar_vector;
next VECTOR;
}
my ($string, $expected) = @$ar_vector;
(my $replaced = $string) =~ s{ ($rx_search) }{$replace{lc $1}}xmsg
+;
is $replaced, $expected, "'$string' -> '$expected'";
}
done_testing;
exit;
__END__
(?msx-i: (?i) (?<! [-\w]) (?: toast | peas | milk | cheese) (?! [-\w])
+ )
# no changes in these
ok 1 - 'milky appease peasoup cheese-toast' -> 'milky appease peasoup
+cheese-toast'
# parts of all these should change
ok 2 - 'mIlK, some PeAs, cheese and toast.' -> 'white, some green, yel
+low and brown.'
1..2
ok 3 - no warnings
1..3
Give a man a fish: <%-{-{-{-<