ThreeMonks has asked for the wisdom of the Perl Monks concerning the following question:
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 @vocabulary = qw(a abc abcd abd bc); my $sentence = 'abdaabc';
In the first and the third cases, only the last matched word get printed, while in the second, a list much longer than expected.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"
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Split a sentence into words
by ikegami (Pope) on May 30, 2009 at 07:05 UTC | |
Re: Split a sentence into words
by akho (Hermit) on May 30, 2009 at 04:45 UTC | |
by bart (Canon) on May 30, 2009 at 12:27 UTC | |
Re: Split a sentence into words
by bart (Canon) on May 30, 2009 at 12:24 UTC |