in reply to Parsing a sentence based on array list
use v5.14; my $string = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit +.'; my @skips = ( 'ipsum', 'sit amet', 'elit' ); my $re = do { local $" = '|'; qr/@skips/; }; say for $string =~ s/$re//gr =~ m/\b(\w+)\b/g;
Output:
Lorem dolor consectetur adipisicing
Update (explanation): The s///r construct returns a new string lacking all "skip" words/phrases. The new string is fed immediately into the second regexp that matches complete words (at least as defined by "\w"). A list is formed, and those are output using 'say' in a loop. If you want to store them instead, change that last line to:
my @keeps = $string =~ s/$re//gr =~ m/\b(\w+)\b/g;
...or...
my $VAR = [ $string =~ s/$re//gr =~ m/\b(\w+)\b/g ];
...if you prefer holding a reference to an anonymous array.
Dave
|
|---|