Course of Raku / Regexes and grammars / Regexes / Captures / Named captures

Quiz — Captures

What does the following program print?

if 'ab12cd' ~~ / (\w+) (\d+) / {
    say "$0 $1";
}
0ab 12
1ab1 2
0ab12 cd
012 ab

The greedy \w+ matches word characters as far as it can while still leaving at least one digit for \d+. It takes ab1, and \d+ then matches the single remaining digit 2. So $0 is ab1 and $1 is 2.

Course navigation

Named captures   |   Non-capturing groups