Hi, monks. I have a vocabulary and sentence, and want to split the sentence into words in the vocabulary. For example,
my @vocabulary = qw(a abc abcd abd bc);
my $sentence = 'abdaabc';
How to get the list (abd, a, abc)?
I tried the following regexes. Each of them match the sentence, but none of them gives the right answer.
my $pattern = join '|', @vocabulary;
$sentence =~ /^($pattern)+$/;
print $1;
my @list1;
$sentence =~ /^(($pattern)(?{push @list1, $^N}))+$/;
print (join ",", @list1), "\n";
use Regexp::DeferredExecution;
my @list2;
$sentence =~ /^(($pattern)(?{push @list2, $^N}))+$/;
print (join ",", @list2), "\n"
In the first and the third cases, only the last matched word get printed, while in the second, a list much longer than expected.