This won't match as there are more words
examples/regex2/matching_word_fails.p6use v6;
my $words = 'foo, bar, moo';
if $words ~~ m/^ \w+ $/ {
say 'no match';
}
so we need to add a regex for the separators , and spaces and we put the whole thing in a [] which is a (non-capturing) grouping, apply a quantifier on that but we also need to add another word matching regex for the last word
examples/regex2/match_several_words.p6use v6;
my $words = 'foo, bar, moo';
if $words ~~ m/^ [\w+\,\s*]* \w+ $/ {
say 'match words';
}
It could be also written this way: The generic quantifier can also get a separator on its right hand side.
examples/regex2/match_several_words2.p6use v6;
my $words = 'foo, bar, moo';
if $words ~~ m/^ [\w+] ** [\,\s*] $/ {
say 'match words again';
}