Course of Raku / Regexes and grammars / Grammars / What is a grammar / Exercises

Year and month

Problem

Declare two named regexes: year, matching exactly four digits, and month, matching exactly two digits. Use both in one pattern to match a string like '2025-06', where a year and a month are separated by a dash. Print the captured year and month, each on its own line.

Example

The program prints:

「2025」
「06」

Solution

See the solution

Course navigation

Solution: Reuse a token   |   Solution: Year and month