Grouping constructs allow you to capture groups of subexpressions and to increase the efficiency of regular expressions with noncapturing lookahead and lookbehind modifiers. The following table describes the Regular Expression Grouping Constructs.
|( )||Captures the matched substring (or noncapturing group; for more information, see the ExplicitCapture option in Regular Expression Options). Captures using () are numbered automatically based on the order of the opening parenthesis, starting from one. The first capture, capture element number zero, is the text matched by the whole regular expression pattern.|
|(?<name> )||Captures the matched substring into a group name or number name. The string used for name must not contain any punctuation and it cannot begin with a number. You can use single quotes instead of angle brackets; for example, |
|(?<name1-name2> )||Balancing group definition. Deletes the definition of the previously defined group name2 and stores in group name1 the interval between the previously defined name2 group and the current group. If no group name2 is defined, the match backtracks. Because deleting the last definition of name2 reveals the previous definition of name2, this construct allows the stack of captures for group name2 to be used as a counter for keeping track of nested constructs such as parentheses. In this construct, name1 is optional. You can use single quotes instead of angle brackets; for example, |
|(?: )||Noncapturing group.|
|(?imnsx-imnsx: )||Applies or disables the specified options within the subexpression. For example, |
|(?= )||Zero-width positive lookahead assertion. Continues match only if the subexpression matches at this position on the right. For example, |
|(?! )||Zero-width negative lookahead assertion. Continues match only if the subexpression does not match at this position on the right. For example, |
|(?<= )||Zero-width positive lookbehind assertion. Continues match only if the subexpression matches at this position on the left. For example, |
|(?<! )||Zero-width negative lookbehind assertion. Continues match only if the subexpression does not match at the position on the left.|
|(?> )||Nonbacktracking subexpression (also known as a "greedy" subexpression). The subexpression is fully matched once, and then does not participate piecemeal in backtracking. (That is, the subexpression matches only strings that would be matched by the subexpression alone.)|
Named captures are numbered sequentially, based on the left-to-right order of the opening parenthesis (like unnamed captures), but numbering of named captures starts after all unnamed captures have been counted. For instance, the pattern
((?<One>abc)/d+)?(?<Two>xyz)(.*) produces the following capturing groups by number and name. (The first capture (number 0) always refers to the entire pattern).
|0||0 (default name)|| |
|1||1 (default name)|| |
|2||2 (default name)|| |