Course of Raku / Regexes and grammars / Grammars / What is a grammar / Exercises / Reuse a token
Solution: Reuse a token
Here is a possible solution to the task.
Code
my regex word { \w+ }
if 'cat dog' ~~ / <word> ' ' <word> / {
say $<word>[0];
say $<word>[1];
}🦋 You can find the source code in the file reuse-a-token.raku.
Output
「cat」
「dog」Comments
The same named regex
wordis called twice in the pattern.When a named capture occurs more than once, the results form a list, so they are read as
$<word>[0]and$<word>[1].
Course navigation
← Reuse a token | Year and month →