Course of Raku / Regexes and grammars / Regexes / Alternations
Alternation
Separate two or more alternatives with a vertical bar |.
The pattern matches if any of them matches:
say 'no thanks' ~~ / yes | no /; # 「no」The pattern tried yes and no; the string
contains no, so that is what matched.
You can list as many alternatives as you like:
say 'the sky is blue' ~~ / red | green | blue /; # 「blue」Alternatives can be any sub-pattern, not just literal words — they
may contain character classes, quantifiers, and captures. To keep an
alternation together inside a larger pattern, group it with square
brackets [ ], which group without
capturing:
say 'cathouse' ~~ / [ cat | dog ] house /; # 「cathouse」Here the alternation cat | dog is one unit, and it must
be followed by house.
Practice
Complete the quiz that covers the contents of this topic.
Course navigation
← Alternations | Quiz — Grouping →
💪 Or jump directly to the exercises in this
section.