newbio has asked for the wisdom of the Perl Monks concerning the following question:
my $line='*Mary* had a little lamb';
my $bagofwords='had |a |Sam |Tom ';
Output: *Mary_had_a* little lamb
I want to combine words that consecutively follow certain tagged words in a sentence. For example, in the sentence above '*Mary* had a little lamb', I would like to match and combine words that consecutively (in a greedy way) follow tagged word *Mary* based on a match with a collection of words I have in $bagofwords variable.
I tried using the following expression but it does not seem to work. Is there equivalent of [^\*\]+ for full words, something like - (had |a |little |Sam |Tom )+ in regular expression?
$line=~s/\*([^\*]+)\*\s($bagofwords)+/*$1_$2*/g;
Thanks.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: greedy match of words
by almut (Canon) on Aug 06, 2009 at 20:44 UTC | |
|
Re: greedy match of words
by GrandFather (Saint) on Aug 06, 2009 at 21:04 UTC | |
|
Re: greedy match of words
by moritz (Cardinal) on Aug 06, 2009 at 20:39 UTC |