πŸ“˜ Regexes (regular expressions) in Perl 6

πŸ“˜ Regexes (regular expressions) in Raku

N. B. Perl 6 has been renamed to Raku. Click to read more.


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>/;

These two matches will print the following.

γ€ŒThu」
Β  weekday => γ€ŒThu」
False

The result of matching is an object of the Match type. When you print it, you will see the matched substring inside small square brackets
γ€Œ…」.

Regexes are the simplest named constructions. Apart from that, rules and tokens exist (and thus, the keywords rule and token).

Tokens are different from rules first regarding how they handle whitespaces. In rules, whitespaces are part of the regexes. In tokens, whitespaces are just visual separators. We will see more about this in the examples below.

my token number_token { <[\d]> <[\d]> }
my rule number_rule { <[\d]> <[\d]> }

(Note that there is no need in semicolons after the closing brace.)

The <[…]> construction creates a character class. In the example above, the two-character string 42 matches with the number_token token but not with the number_rule rule.

say 1 if "42" ~~ /<number_token>/;
say 1 if "4 2" ~~ /<number_rule>/;

Leave a Reply

Your email address will not be published. Required fields are marked *

Retype the CAPTCHA code from the image
Change the CAPTCHA codeSpeak the CAPTCHA code