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

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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s