Raku Books / Perl 6 at a Glance / Regexes and Grammars

Regexes

In fact, Perl 6 just calls regular expressions regexes. The basic syntax is a bit different from Perl 5, but most elements (such as quantifiers * or +) still look familiar. The regex keyword is used to build a regex. Let us create a regex for the short names of weekdays.

my regex weekday
    {[Mon | Tue | Wed | Thu | Fri | Sat | Sun]};

The square brackets are enclosing the list of alternatives.

You can use the named regex inside other regexes by referring to its name in a pair of angle brackets. To match the string against a regex, use the smartmatch operator (~~).

say 'Thu' ~~ m/<weekday>/;
say 'Thy' ~~ m/<weekday>/;

CodeBlockPlaceholder3 「Thu」 weekday => 「Thu」 False CodeBlockPlaceholder4raku-static my token number_token { <[> <[> } my rule number_rule { <[> <[> } CodeBlockPlaceholder5raku-static say 1 if “42” ~~ //; say 1 if “4 2” ~~ //; CodeBlockPlaceholder6

Course navigation

Regexes and Grammars   |   The $/ object