Regexes & grammars

Substitution

Full

Replace matched text in place with s///, or every occurrence with s:g///.

s/pattern/replacement/ finds the pattern and replaces it. Applied with ~~ to a mutable variable, it edits that variable in place. By default it replaces the first match; the :g adverb replaces them all.

First match #

my $s = "foo";
$s ~~ s/o/0/;
say $s;
Output
f0o

Only the first o is replaced, giving f0o.

Global substitution #

my $s = "foo";
$s ~~ s:g/o/0/;
say $s;
Output
f00

Notes #