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

RegexBuddy—Better than a regular expression reference!

Regex Reference: Balancing Groups, Recursion, and Subroutines

FeatureSyntaxDescriptionExample 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?
(\k<l>(?<-l>))+
(?(l)(?!))$
matches any palindrome word
V2YESnononononononononononononononononononononono
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?
(\k'l'(?'-l'))+
(?(l)(?!))$
matches any palindrome word
V2YESnononononononononononononononononononononono
Recursion (?R) Recursion of the entire regular expression. a(?R)?z matches az, aazz, aaazzz, etc. V2nono5.10YESYESYESYESYESnonononononoECMA
1.42–1.64
nononononononono
Recursion (?0) Recursion of the entire regular expression. a(?0)?z matches az, aazz, aaazzz, etc. V2nono5.10YESYESYESYESYESnonononononoECMA
1.42–1.64
nononononononono
Recursion \g<0> Recursion of the entire regular expression. a\g<0>?z matches az, aazz, aaazzz, etc. V2nonono7.7YES5.2.7YESYESnononono2.0nononononononononono
Recursion \g'0' Recursion of the entire regular expression. a\g'0'?z matches az, aazz, aaazzz, etc. V2nonono7.7YES5.2.7YESYESnononono2.0nononononononononono
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. V2no4 only5.10YESYESYESYESYESnonononononoECMA
1.42–1.64
nononononononono
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. V2nonono7.7YES5.2.7YESYESnononono1.9nononononononononono
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. V2nonono7.7YES5.2.7YESYESnononono1.9nononononononononono
Relative subroutine call (?-1) where -1 is 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. V2nono5.107.2YES5.2.4YESYESnonononononoECMA
1.42–1.64
nononononononono
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. V2nonono7.7YES5.2.7YESYESnononono1.9nononononononononono
Relative subroutine call \g'-1' where -1 is 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. V2nonono7.7YES5.2.7YESYESnononono1.9nononononononononono
Forward subroutine call (?+1) where +1 is 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 V2nono5.107.2YES5.2.4YESYESnonononononoECMA
1.42–1.64
nononononononono
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 V2nonono7.7YES5.2.7YESYESnononono2.0nononononononononono
Forward subroutine call \g'+1' where +1 is 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 V2nonono7.7YES5.2.7YESYESnononono2.0nononononononononono
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. V2nono5.107.0YES5.2.2YESYESnonononononoECMA
1.42–1.64
nononononononono
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. V2nono5.10YESYESYESYESYESnonononononoECMA
1.42–1.64
nononononononono
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. V2nonono7.7YES5.2.7YESYESnononono1.9nononononononononono
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. V2nonono7.7YES5.2.7YESYESnononono1.9nononononononononono
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]))
x(?1)y(?1)z
matches xayaz, xaybz, xbyaz, and xbybz
V2nono5.107.0YES5.2.2YESYESnonononononoECMA
1.42–1.64
nononononononono
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. V2n/an/an/anononononon/an/an/an/a1.9n/an/an/an/an/an/an/an/an/an/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. non/anononononononon/an/an/an/an/an/anon/an/an/an/an/an/an/an/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). non/ano5.10–5.18nononononon/an/an/an/anon/anon/an/an/an/an/an/an/an/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 non/an/an/a7.7YES5.2.7YESYESn/an/an/an/anon/an/an/an/an/an/an/an/an/an/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 V2n/ano5.10YESYESYESYESYESn/an/an/an/an/an/aECMA
1.42–1.64
n/an/an/an/an/an/an/an/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 V2n/an/an/anononononon/an/an/an/a1.9n/an/an/an/an/an/an/an/an/an/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 non/a4 onlynonononononon/an/an/an/an/an/anon/an/an/an/an/an/an/an/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 V2n/an/ano6.5YES5.1.3YESYESn/an/an/an/an/an/anon/an/an/an/an/an/an/an/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. non/an/ano6.5YES5.1.3YESYESn/an/an/an/anon/aECMA
1.42–1.64
n/an/an/an/an/an/an/an/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 non/a4 onlyno6.5YES5.1.3YESYESn/an/an/an/anon/anon/an/an/an/an/an/an/an/a
FeatureSyntaxDescriptionExample 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

Make a Donation

Did this website just save you a trip to the bookstore? Please make a donation to support this site, and you'll get a lifetime of advertisement-free access to this site!

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 |