Course of Raku / Regexes and grammars / Regexes / Matching strings
The smartmatch operator
The operator that applies a regex to a string is the smartmatch
~~:
say 'the cat sat' ~~ /cat/; # 「cat」A pattern written between slashes is the most common form, but there are two more that mean the same thing and are sometimes clearer:
say 'the cat sat' ~~ m/cat/; # 「cat」
say 'the cat sat' ~~ rx/cat/; # 「cat」The m/.../ form emphasises that you are
matching, and is handy when you want to add options to the
match (you will meet those options, called adverbs, later). The
rx/.../ form constructs a regex value without matching it
yet.
Very often you only care whether the string matched, not what exactly
was found. Because a successful match is a true value and a failed match
is false, you can use the result directly in a Boolean context. The
cleanest way to get a plain True or False is
the so function:
say so 'the cat sat' ~~ /cat/; # True
say so 'the cat sat' ~~ /dog/; # FalseThis makes a regex a natural condition for if:
if 'the cat sat' ~~ /cat/ {
say 'found it'; # found it
}Practice
Complete the quiz that covers the contents of this topic.
Course navigation
← Matching strings | Quiz — Smartmatch →
💪 Or jump directly to
the exercises in this section.