Course of Raku / Regexes and grammars / Grammars / What is a grammar / Exercises / Year and month
Solution: Year and month
Here is a possible solution to the task.
Code
my regex year { \d ** 4 }
my regex month { \d ** 2 }
if '2025-06' ~~ / <year> '-' <month> / {
say $<year>;
say $<month>;
}🦋 You can find the source code in the file year-token.raku.
Output
「2025」
「06」Comments
Two named regexes are declared, each describing one piece of the date: a four-digit
yearand a two-digitmonth.The pattern combines them with a literal dash between, and each is captured under its own name. Building a larger pattern from small named parts like this is exactly the idea a grammar formalises.