Modifiers

The previous example to match a list of words was:

    m/(\w+) ** [\,\s*] \s* 'and' \s* (\w+)/

This works, but the repeated "I don't care about whitespace" units are clumsy. The desire to allow whitespace anywhere in a string is common. Perl 6 regexes allow this through the use of the :sigspace modifier (shortened to :s):

    my $ingredients = 'eggs, milk, sugar and flour';

    if $ingredients ~~ m/:s ( \w+ ) ** \,'and' (\w+)/ {
        say 'list: ', $/[0].join(' | ');
        say 'end:  ', $/[1];
    }

This modifier allows optional whitespace in the text wherever there one or more whitespace characters appears in the pattern. It's even a bit cleverer than that: between two word characters whitespace is mandatory. The regex does not match the string eggs, milk, sugarandflour.

The :ignorecase or :i modifier makes the regex insensitive to upper and lower case, so m/ :i perl / matches perl, PerL, and PERL (though who names a programming language in all uppercase letters?)