Regexes & grammars

Separated quantifiers % and %%

Full

Match a repeated pattern with a separator between the repeats, in one construct.

The % modifier on a quantifier matches a repeated atom separated by another pattern — the clean way to parse comma- or dash-delimited lists without writing the separator logic by hand. %% is the variant that also allows a trailing separator.

A separated list #

(\d)+ % "-" matches one or more digits separated by -, capturing the digits.

if "1-2-3" ~~ / (\d)+ % "-" / { say $0.join(",") }
Output
1,2,3

The + % "-" says "one or more, with - between them" — so the separators are consumed but not captured.

Named atoms #

The atom can be a subrule, which is how a grammar parses a delimited list.

my token num { \d+ }
"10,20,30" ~~ / <num>+ % "," /;
say $<num>.map(~*).join("|");
Output
10|20|30

Notes #