In Regexes§
See primary documentation in context for Lexical conventions 0
Substitutions are written similarly to matching, but the substitution operator has both an area for the regex to match, and the text to substitute:
s/replace/with/; # a substitution that is applied to $_ $str ~~ s/replace/with/; # a substitution applied to a scalar
The substitution operator allows delimiters other than the slash:
s|replace|with|; s!replace!with!; s,replace,with,;
Note that neither the colon :
nor balancing delimiters such as {}
or ()
can be substitution delimiters. Colons clash with adverbs such as s:i/Foo/bar/
and the other delimiters are used for other purposes.
If you use balancing curly braces, square brackets, or parentheses, the substitution works like this instead:
s[replace] = 'with';
The right-hand side is now a (not quoted) Raku expression, in which $/
is available as the current match:
$_ = 'some 11 words 21'; s:g[ \d+ ] = 2 * $/; .say; # OUTPUT: «some 22 words 42»
Like the m//
operator, whitespace is ignored in the regex part of a substitution.