In Regexes§

See primary documentation in context for Non-capturing grouping

The parentheses in regexes perform a double role: they group the regex elements inside and they capture what is matched by the sub-regex inside.

To get only the grouping behavior, you can use square brackets [ ... ] which, by default, don't capture.

if 'abc' ~~ / [a||b] (c) / {
    say ~$0;                # OUTPUT: «c␤» 
}

If you do not need the captures, using non-capturing [ ... ] groups provides the following benefits:

  • they more cleanly communicate the regex intent,

  • they make it easier to count the capturing groups that do match, and

  • they make matching a bit faster.