c:\@Work\Perl>perl -le "use 5.010; ;; use warnings; use strict; ;; my %direct_substitution = ( 'apple' => 'PEAR', 'red' => 'YELLOW', 'xyzzy' => 'SOME OTHER THING', ); ;; my ($search) = map qr{ \b (?: $_ ) \b }xms, join q{ | }, keys %direct_substitution ; print $search; ;; my @lines = ( 'apple xapple apple applex xapplex apple', 'red', 'xxx red apple yyyy xyzzy zz', ); ;; my $count = 0; $count += s{ ($search) }{$direct_substitution{$1}}xmsg for @lines; ;; print qq{substitutions: $count}; print qq{'$_'} for @lines; " (?^msx: \b (?: apple | red | xyzzy ) \b ) substitutions: 7 'PEAR xapple PEAR applex xapplex PEAR' 'YELLOW' 'xxx YELLOW PEAR yyyy SOME OTHER THING zz'