in reply to greedy match of words
I can't see a straight forward way to do it with a single regex, but the following may suit:
use strict; use warnings; my $line='*Mary* had a little lamb'; my $bagofwords='had |a |Sam |Tom '; $line = join ' ', map {s/(\*.*\* | ^(?:$bagofwords)$)/*$1*/x; s/\*\*/* +/g; $_} $line =~ /(\S+)/g; $line =~ s/\* \*/_/g; print $line;
Prints:
*Mary_had_a* little lamb
|
|---|