Course of Raku / Regexes and grammars / Regexes / Alternations / Exercises

Word or digit

Problem

Alternatives in an alternation do not have to be literal words — they can be any sub-pattern. Write a pattern that matches either the literal word cat or a single digit (\d). Test it on the string 'item 5' and print the match.

Example

The program prints:

「5」

Solution

See the solution

Course navigation

Quiz — Alternations   |   Solution: Word or digit