Replacement Reference |

Characters |

Matched Text & Backreferences |

Context & Case Conversion |

Conditionals |

Feature | Syntax | Description | Example | JGsoft | .NET | Java | Perl | PCRE | PCRE2 | PHP | Delphi | R | JavaScript | VBScript | XRegExp | Python | Ruby | std::regex | Boost | Tcl ARE | POSIX BRE | POSIX ERE | GNU BRE | GNU ERE | Oracle | XML | XPath |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|

Balancing group | (?<capture-subtract>regex) where “capture” and “subtract” are group names and “regex” is any regex |
The name “subtract” must be used as the name of a capturing group elsewhere in the regex. If this group has captured matches that haven’t been subtracted yet, then the balancing group subtracts one capture from “subtract”, attempts to match “regex”, and stores its match into the group “capture”. If “capture” is omitted, the same happens without storing the match. If “regex” is omitted, the balancing group succeeds without advancing through the string. If the group “subtract” has no matches to subtract, then the balancing group fails to match, regardless of whether “regex” is specified or not. | ^(?<l>\w)+\w? matches any palindrome word(\k<l>(?<-l>))+ (?(l)(?!))$ |
V2 | YES | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no |

Balancing group | (?'capture-subtract'regex) where “capture” and “subtract” are group names and “regex” is any regex |
The name “subtract” must be used as the name of a capturing group elsewhere in the regex. If this group has captured matches that haven’t been subtracted yet, then the balancing group subtracts one capture from “subtract”, attempts to match “regex”, and stores its match into the group “capture”. If “capture” is omitted, the same happens without storing the match. If “regex” is omitted, the balancing group succeeds without advancing through the string. If the group “subtract” has no matches to subtract, then the balancing group fails to match, regardless of whether “regex” is specified or not. | ^(?'l'\w)+\w? matches any palindrome word(\k'l'(?'-l'))+ (?(l)(?!))$ |
V2 | YES | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no |

Recursion | (?R) |
Recursion of the entire regular expression. | a(?R)?z matches az, aazz, aaazzz, etc. |
V2 | no | no | 5.10 | YES | YES | YES | YES | YES | no | no | no | no | no | no | ECMA 1.42–1.83 | no | no | no | no | no | no | no | no |

Recursion | (?0) |
Recursion of the entire regular expression. | a(?0)?z matches az, aazz, aaazzz, etc. |
V2 | no | no | 5.10 | YES | YES | YES | YES | YES | no | no | no | no | no | no | ECMA 1.42–1.83 | no | no | no | no | no | no | no | no |

Recursion | \g<0> |
Recursion of the entire regular expression. | a\g<0>?z matches az, aazz, aaazzz, etc. |
V2 | no | no | no | 7.7 | YES | 5.2.7 | YES | YES | no | no | no | no | 2.0 | no | no | no | no | no | no | no | no | no | no |

Recursion | \g'0' |
Recursion of the entire regular expression. | a\g'0'?z matches az, aazz, aaazzz, etc. |
V2 | no | no | no | 7.7 | YES | 5.2.7 | YES | YES | no | no | no | no | 2.0 | no | no | no | no | no | no | no | no | no | no |

Subroutine call | (?1) where 1 is the number of a capturing group |
Recursion of a capturing group or subroutine call to a capturing group. | a(b(?1)?y)z matches abyz, abbyyz, abbbyyyz, etc. |
V2 | no | 4 only | 5.10 | YES | YES | YES | YES | YES | no | no | no | no | no | no | ECMA 1.42–1.83 | no | no | no | no | no | no | no | no |

Subroutine call | \g<1> where 1 is the number of a capturing group |
Recursion of a capturing group or subroutine call to a capturing group. | a(b\g<1>?y)z matches abyz, abbyyz, abbbyyyz, etc. |
V2 | no | no | no | 7.7 | YES | 5.2.7 | YES | YES | no | no | no | no | 1.9 | no | no | no | no | no | no | no | no | no | no |

Subroutine call | \g'1' where 1 is the number of a capturing group |
Recursion of a capturing group or subroutine call to a capturing group. | a(b\g'1'?y)z matches abyz, abbyyz, abbbyyyz, etc. |
V2 | no | no | no | 7.7 | YES | 5.2.7 | YES | YES | no | no | no | no | 1.9 | no | no | no | no | no | no | no | no | no | no |

Relative subroutine call | (?-1) where -1 is a negative integer |
Recursion of or subroutine call to a capturing group that can be found by counting as many opening parentheses of named or numbered capturing groups as specified by the number from right to left starting at the subroutine call. | a(b(?-1)?y)z matches abyz, abbyyz, abbbyyyz, etc. |
V2 | no | no | 5.10 | 7.2 | YES | 5.2.4 | YES | YES | no | no | no | no | no | no | ECMA 1.42–1.83 | no | no | no | no | no | no | no | no |

Relative subroutine call | \g<-1> where -1 is a negative integer |
Recursion of or subroutine call to a capturing group that can be found by counting as many opening parentheses of named or numbered capturing groups as specified by the number from right to left starting at the subroutine call. | a(b\g<-1>?y)z matches abyz, abbyyz, abbbyyyz, etc. |
V2 | no | no | no | 7.7 | YES | 5.2.7 | YES | YES | no | no | no | no | 1.9 | no | no | no | no | no | no | no | no | no | no |

Relative subroutine call | \g'-1' where -1 is a negative integer |
Recursion of or subroutine call to a capturing group that can be found by counting as many opening parentheses of named or numbered capturing groups as specified by the number from right to left starting at the subroutine call. | a(b\g'-1'?y)z matches abyz, abbyyz, abbbyyyz, etc. |
V2 | no | no | no | 7.7 | YES | 5.2.7 | YES | YES | no | no | no | no | 1.9 | no | no | no | no | no | no | no | no | no | no |

Forward subroutine call | (?+1) where +1 is a positive integer |
Recursion of or subroutine call to a capturing group that can be found by counting as many opening parentheses of named or numbered capturing groups as specified by the number from left to right starting at the subroutine call. | (?+1)x([ab]) matches axa, axb, bxa, and bxb |
V2 | no | no | 5.10 | 7.2 | YES | 5.2.4 | YES | YES | no | no | no | no | no | no | ECMA 1.42–1.83 | no | no | no | no | no | no | no | no |

Forward subroutine call | \g<+1> where +1 is a positive integer |
Recursion of or subroutine call to a capturing group that can be found by counting as many opening parentheses of named or numbered capturing groups as specified by the number from left to right starting at the subroutine call. | \g<+1>x([ab]) matches axa, axb, bxa, and bxb |
V2 | no | no | no | 7.7 | YES | 5.2.7 | YES | YES | no | no | no | no | 2.0 | no | no | no | no | no | no | no | no | no | no |

Forward subroutine call | \g'+1' where +1 is a positive integer |
Recursion of or subroutine call to a capturing group that can be found by counting as many opening parentheses of named or numbered capturing groups as specified by the number from left to right starting at the subroutine call. | \g'+1'x([ab]) matches axa, axb, bxa, and bxb |
V2 | no | no | no | 7.7 | YES | 5.2.7 | YES | YES | no | no | no | no | 2.0 | no | no | no | no | no | no | no | no | no | no |

Named subroutine call | (?&name) where “name” is the name of a capturing group |
Recursion of a capturing group or subroutine call to a capturing group. | a(?<x>b(?&x)?y)z matches abyz, abbyyz, abbbyyyz, etc. |
V2 | no | no | 5.10 | 7.0 | YES | 5.2.2 | YES | YES | no | no | no | no | no | no | ECMA 1.42–1.83 | no | no | no | no | no | no | no | no |

Named subroutine call | (?P>name) where “name” is the name of a capturing group |
Recursion of a capturing group or subroutine call to a capturing group. | a(?P<x>b(?P>x)?y)z matches abyz, abbyyz, abbbyyyz, etc. |
V2 | no | no | 5.10 | YES | YES | YES | YES | YES | no | no | no | no | no | no | ECMA 1.42–1.83 | no | no | no | no | no | no | no | no |

Named subroutine call | \g<name> where “name” is the name of a capturing group |
Recursion of a capturing group or subroutine call to a capturing group. | a(?<x>b\g<x>?y)z matches abyz, abbyyz, abbbyyyz, etc. |
V2 | no | no | no | 7.7 | YES | 5.2.7 | YES | YES | no | no | no | no | 1.9 | no | no | no | no | no | no | no | no | no | no |

Named subroutine call | \g'name' where “name” is the name of a capturing group |
Recursion of a capturing group or subroutine call to a capturing group. | a(?'x'b\g'x'?y)z matches abyz, abbyyz, abbbyyyz, etc. |
V2 | no | no | no | 7.7 | YES | 5.2.7 | YES | YES | no | no | no | no | 1.9 | no | no | no | no | no | no | no | no | no | no |

Subroutine definitions | (?(DEFINE)regex) where “regex” is any regex |
The DEFINE group does not take part in the matching process. Subroutine calls can be made to capturing groups inside the DEFINE group. | (?(DEFINE)([ab])) matches x(?1)y(?1)z xayaz, xaybz, xbyaz, and xbybz |
V2 | no | no | 5.10 | 7.0 | YES | 5.2.2 | YES | YES | no | no | no | no | no | no | ECMA 1.42–1.83 | no | no | no | no | no | no | no | no |

Subroutine calls capture | Subroutine call using Ruby-style \g syntax |
A subroutine call to a capturing group makes that capturing group store the text matched during the subroutine call. | When ([ab])\g'1' matches ab the first capturing group holds b after the match. |
V2 | n/a | n/a | n/a | no | no | no | no | no | n/a | n/a | n/a | n/a | 1.9 | n/a | n/a | n/a | n/a | n/a | n/a | n/a | n/a | n/a | n/a |

Subroutine calls capture | Subroutine call using syntax other than \g |
A subroutine call to a capturing group makes that capturing group store the text matched during the subroutine call. | When ([ab])(?1) matches ab the first capturing group holds b after the match. |
no | n/a | no | no | no | no | no | no | no | n/a | n/a | n/a | n/a | n/a | n/a | no | n/a | n/a | n/a | n/a | n/a | n/a | n/a | n/a |

Recursion isolates capturing groups | Any recursion or subroutine call | Each subroutine call has its own separate storage space for capturing groups. Backreferences inside the call cannot see text matched before the call, and backreferences after the call cannot see text matched inside the call. Backreferences inside recursion cannot see text matched at other recursion levels. | (a)(\1)(?2) never matches anything because \1 always fails during the call made by (?2). |
no | n/a | no | 5.10–5.18 | no | no | no | no | no | n/a | n/a | n/a | n/a | no | n/a | no | n/a | n/a | n/a | n/a | n/a | n/a | n/a | n/a |

Recursion reverts capturing groups | Recursion or subroutine call using Ruby-style \g syntax |
When the regex engine exits from recursion or a subroutine call, it reverts all capturing groups to the text they had matched prior to entering the recursion or subroutine call. | When (a)(([bc])\1)\g'2' matches abaca the third group stores b after the match |
no | n/a | n/a | n/a | 7.7 | YES | 5.2.7 | YES | YES | n/a | n/a | n/a | n/a | no | n/a | n/a | n/a | n/a | n/a | n/a | n/a | n/a | n/a | n/a |

Recursion reverts capturing groups | Recursion or subroutine call using syntax other than \g |
When the regex engine exits from recursion or a subroutine call, it reverts all capturing groups to the text they had matched prior to entering the recursion or subroutine call. | When (a)(([bc])\1)(?2) matches abaca the third group stores b after the match |
V2 | n/a | no | 5.10 | YES | YES | YES | YES | YES | n/a | n/a | n/a | n/a | n/a | n/a | ECMA 1.42–1.83 | n/a | n/a | n/a | n/a | n/a | n/a | n/a | n/a |

Recursion does not isolate or revert capturing groups | Recursion or subroutine call using Ruby-style \g syntax |
Capturing groups are not given any special treatment by recursion and subroutine calls, except perhaps that subroutine calls capture. Backreferences always see the text most recently matched by each capturing group, regardless of whether they are inside the same level of recursion or not. | When (a)(([bc])\1)\g'2' matches abaca the third group stores c after the match |
V2 | n/a | n/a | n/a | no | no | no | no | no | n/a | n/a | n/a | n/a | 1.9 | n/a | n/a | n/a | n/a | n/a | n/a | n/a | n/a | n/a | n/a |

Recursion does not isolate or revert capturing groups | Recursion or subroutine call using syntax other than \g |
Capturing groups are not given any special treatment by recursion and subroutine calls, except perhaps that subroutine calls capture. Backreferences always see the text most recently matched by each capturing group, regardless of whether they are inside the same level of recursion or not. | When (a)(([bc])\1)(?2) matches abaca the third group stores c after the match |
no | n/a | 4 only | no | no | no | no | no | no | n/a | n/a | n/a | n/a | n/a | n/a | no | n/a | n/a | n/a | n/a | n/a | n/a | n/a | n/a |

Recursion is atomic | Recursion or subroutine call using (?P>…) |
Recursion and subroutine calls are atomic. Once the regex engine exits from them, it will not backtrack into it to try different permutations of the recursion or subroutine call. | (a+)(?P>1)(?P>1) can never match anything because the first (?P>1) matches all remaining a’s and the regex engine won’t backtrack into the first (?P>1) when the second one fails |
V2 | n/a | n/a | no | 6.5 | 10.00–10.23 | 5.1.3–7.2.34 | YES | 2.14.0–3.6.3 | n/a | n/a | n/a | n/a | n/a | n/a | no | n/a | n/a | n/a | n/a | n/a | n/a | n/a | n/a |

Recursion is atomic | Recursion of the whole regex using syntax other than (?P>0) |
Recursion of the whole regex is atomic. Once the regex engine exits from recursion, it will not backtrack into it to try different permutations of the recursion. | aa$|a(?R)a|a matches a in aaa when recursion is atomic; otherwise it would match the whole string. |
no | n/a | n/a | no | 6.5 | YES | 5.1.3–7.2.34 | YES | YES | n/a | n/a | n/a | n/a | no | n/a | ECMA 1.42–1.83 | n/a | n/a | n/a | n/a | n/a | n/a | n/a | n/a |

Recursion is atomic | Subroutine call using syntax other than (?P>…) |
Subroutine calls are atomic. Once the regex engine exits from them, it will not backtrack into it to try different permutations of the subroutine call. | (a+)(?1)(?1) can never match anything because the first (?1) matches all remaining a’s and the regex engine won’t backtrack into the first (?1) when the second one fails |
no | n/a | 4 only | no | 6.5 | 10.00–10.23 | 5.1.3–7.2.34 | YES | 2.14.0–3.6.3 | n/a | n/a | n/a | n/a | no | n/a | no | n/a | n/a | n/a | n/a | n/a | n/a | n/a | n/a |

Feature | Syntax | Description | Example | JGsoft | .NET | Java | Perl | PCRE | PCRE2 | PHP | Delphi | R | JavaScript | VBScript | XRegExp | Python | Ruby | std::regex | Boost | Tcl ARE | POSIX BRE | POSIX ERE | GNU BRE | GNU ERE | Oracle | XML | XPath |

| Quick Start | Tutorial | Tools & Languages | Examples | Reference | Book Reviews |

| Introduction | Table of Contents | Quick Reference | Characters | Basic Features | Character Classes | Shorthands | Anchors | Word Boundaries | Quantifiers | Unicode | Capturing Groups & Backreferences | Named Groups & Backreferences | Special Groups | Mode Modifiers | Recursion & Balancing Groups |

| Characters | Matched Text & Backreferences | Context & Case Conversion | Conditionals |

Page URL: https://www.regular-expressions.info/refrecurse.html

Page last updated: 16 October 2023

Site last updated: 07 February 2024

Copyright © 2003-2024 Jan Goyvaerts. All rights reserved.