Camelia
Prev: Quantifier 2

Match several words

This won't match as there are more words

examples/regex2/matching_word_fails.p6
use 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.p6
use 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.p6
use v6;

my $words = 'foo, bar, moo';

if $words ~~ m/^ [\w+] ** [\,\s*] $/ {
    say 'match words again';
}


Prev: Quantifier 2

Table of Contents

Comments

In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation.
comments powered by Disqus

Perl 6 Tricks and Treats newsletter

Register to the free newsletter now, and get updates and news.
Email:
Name: