Regexes & grammars
Regex adverbs — :i / :s / :g
FullModify how a match works — case-insensitive, significant-space, global.
Adverbs tweak a match. Written after the m/s/rx (or on ~~), they turn on case-insensitivity (:i), significant whitespace (:s), global matching (:g), and more.
:i — case-insensitive #
say so "HELLO" ~~ m:i/hello/; say so "HELLO" ~~ /hello/;
Output
True
FalseWithout :i, the literal hello doesn't match HELLO.
:s — significant space #
By default whitespace in a regex is ignored. :s (sigspace) makes each space match real whitespace (\s+), so patterns can be laid out with meaningful gaps.
say so "foo bar" ~~ m:s/foo bar/;
Output
True:g — global #
:g finds every match, returning a list of them.
say ("a1b2c3" ~~ m:g/\d/).elems;
Output
3Notes #
- Adverbs stack:
m:i:g/…/is both case-insensitive and global. :gon a substitution (s:g/…/…/) replaces every occurrence — see Substitution.- Other useful ones:
:r(ratchet, no backtracking — the default fortoken),:x(n)(match exactly n times),:nth(k).