Regexes & grammars

Matching with ~~

Full

Test a string against a regex; the result is a Match object (truthy) or Nil.

A regex is written between / … /. Smartmatching a string against one with ~~ runs the regex and returns a Match object on success (falsy Nil on failure). The Match stringifies to the matched text.

A basic match #

say "hello" ~~ /ell/;
Output
「ell」

The 「 」 corner brackets are how a Match displays — they delimit the matched substring, here ell.

Match as a Boolean #

In Boolean context a Match is true and a failed match is false. so forces that Boolean view.

say so "hello" ~~ /xyz/;
say so "hello" ~~ /ell/;
Output
False
True

Notes #