Next: , Previous: Grouping Operators, Up: Common Operators


14.3.8 The Back-reference Operator (\digit)

If the syntax bit RE_NO_BK_REF isn't set, then Regex recognizes back references. A back reference matches a specified preceding group. The back reference operator is represented by ‘\digit’ anywhere after the end of a regular expression's digit-th group (see Grouping Operators).

digit must be between ‘1’ and ‘9’. The matcher assigns numbers 1 through 9 to the first nine groups it encounters. By using one of ‘\1’ through ‘\9’ after the corresponding group's close-group operator, you can match a substring identical to the one that the group does.

Back references match according to the following (in all examples below, ‘(’ represents the open-group, ‘)’ the close-group, ‘{’ the open-interval and ‘}’ the close-interval operator):

You can use a back reference as an argument to a repetition operator. For example, ‘(a(b))\2*’ matches ‘a’ followed by two or more ‘b’s. Similarly, ‘(a(b))\2{3}’ matches ‘abbbb’.

If there is no preceding digit-th subexpression, the regular expression is invalid.