|
We noted in Part 1 that groupings () had two distinct functions: 1) group regexp
elements together as a single unit, and 2) extract, or capture, substrings that matched the
regexp in the grouping. Non-capturing groupings, denoted by (?:regexp), allow the
regexp to be treated as a single unit, but don't extract substrings or set matching variables $1,
etc. Both capturing and non-capturing groupings are allowed to co-exist in the same regexp.
Because there is no extraction, non-capturing groupings are faster than capturing groupings.
Non-capturing groupings are also handy for choosing exactly which parts of a regexp are to be
extracted to matching variables:
# match a number, $1-$4 are set, but we only want $1
/([+-]?\ *(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?)/;
# match a number faster , only $1 is set
/([+-]?\ *(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?)/;
# match a number, get $1 = whole number, $2 = exponent
/([+-]?\ *(?:\d+(?:\.\d*)?|\.\d+)(?:[eE]([+-]?\d+))?)/;
|
|
Non-capturing groupings are also useful for removing nuisance elements gathered from a split
operation:
$x = '12a34b5';
@num = split /(a|b)/, $x; # @num = ('12','a','34','b','5')
@num = split /(?:a|b)/, $x; # @num = ('12','34','5')
|
|
Non-capturing groupings may also have embedded modifiers: (?i-m:regexp) is a
non-capturing grouping that matches regexp case insensitively and turns off
multi-line mode.
|
|