In Regexes§
See primary documentation in context for Subrules
Just like you can put pieces of code into subroutines, you can also put pieces of regex into named rules.
myif "abc\ndef" ~~ / def/
A named regex can be declared with my regex named-regex { body here }
, and called with <named-regex>
. At the same time, calling a named regex installs a named capture with the same name.
To give the capture a different name from the regex, use the syntax <capture-name=named-regex>
. If no capture is desired, a leading dot or ampersand will suppress it: <.named-regex>
if it is a method declared in the same class or grammar, <&named-regex>
for a regex declared in the same lexical context.
Here's more complete code for parsing ini
files:
mymymymymy =my ;if ~~ /*/say .raku;# OUTPUT: «{:passwords(${:jack("password1"), :joy("muchmoresecure123")}),# :quotas(${:jack("123"), :joy("42")})}»
Named regexes can and should be grouped in grammars. A list of predefined subrules is here.