Quick Start
Tutorial
Tools & Languages
Examples
Reference
Book Reviews
RegexBuddy Instantly and accurately analyze and test any regular expression with RegexBuddy. Save yourself the headache of figuring out the differences between regex flavors. RegexBuddy knows all the regex features and regex flavors listed in this reference. RegexBuddy also explains and emulates many more subtle differences and idiosyncrasies that are omitted here to keep this reference readable. Get your own copy of RegexBuddy now

Regular Expression Reference: Special Groups

FeatureSyntaxDescriptionExample JGsoft .NET Java Perl PCRE PHP Delphi R JavaScript VBScript XRegExp Python Ruby std::regex Tcl ARE POSIX BRE POSIX ERE GNU BRE GNU ERE Oracle XML XPath
Comment (?#comment) Everything between (?# and ) is ignored by the regex engine. a(?#foobar)b matches ab YESYESnoYESYESYESYESYESnonoYESYESYESnoYESnonononononono
Branch reset group (?|regex) If the regex inside the branch reset group has multiple alternatives with capturing groups, then the capturing group numbers are the same in all the alternatives. (x)(?|(a)|(bc)|(def))\2 matches xaa, xbcbc, or xdefdef with the first group capturing x and the second group capturing a, bc, or def nonono5.107.25.2.4YESYESnononononononononononononono
Atomic group (?>regex) Atomic groups prevent the regex engine from backtracking back into the group after a match has been found for the group. If the remainder of the regex fails, the engine may backtrack over the group if a quantifier or alternation makes it optional. But it will not backtrack into the group to try other permutations of the group. a(?>bc|b)c matches abcc but not abc YESYESYESYESYESYESYESYESnonononoYESnonononononononono
Positive lookahead (?=regex) Matches at a position where the pattern inside the lookahead can be matched. Matches only the position. It does not consume any characters or expand the match. In a pattern like one(?=two)three, both two and three have to match at the position where the match of one ends. t(?=s) matches the second t in streets. YESYESYESYESYESYESYESYESYESYESYESYESYESECMAYESnonononononono
Negative lookahead (?!regex) Similar to positive lookahead, except that negative lookahead only succeeds if the regex inside the lookahead fails to match. t(?!s) matches the first t in streets. YESYESYESYESYESYESYESYESYESYESYESYESYESECMAYESnonononononono
Positive lookbehind (?<=regex) Matches at a position if the pattern inside the lookbehind can be matched ending at that position. (?<=s)t matches the first t in streets. YESYESYESYESYESYESYESYESnononoYES1.9nonononononononono
Negative lookbehind (?<!regex) Matches at a position if the pattern inside the lookbehind cannot be matched ending at that position. (?<!s)t matches the second t in streets. YESYESYESYESYESYESYESYESnononoYES1.9nonononononononono
Lookbehind (?<=regex|longer regex) Alternatives inside lookbehind can differ in length. (?<=is|e)t matches the second and fourth t in twisty streets. YESYESYESnoYESYESYESYESn/an/an/ano1.9n/an/an/an/an/an/an/an/an/a
Lookbehind (?<=x{n,m}) Quantifiers with a finite maximum number of repetitions can be used inside lookbehind. (?<=s\w{1,7})t matches only the fourth t in twisty streets. YESYES6
4 fail
nononononon/an/an/anonon/an/an/an/an/an/an/an/an/a
Lookbehind (?<=regex) The full regular expression syntax can be used inside lookbehind. (?<=s+t matches only the fourth t in twisty streets. YESYESnonononononon/an/an/anonon/an/an/an/an/an/an/an/an/a
Keep text out of the regex match \K The text matched by the part of the regex to the left of the \K is omitted from the overall regex match. Other than that the regex is matched normally from left to right. Capturing groups to the left of the \K capture as usual. s\Kt matches only the first t in streets. nonono5.107.25.2.4YESYESnononono2.0nonononononononono
Lookaround conditional (?(?=regex)then|else) where (?=regex) is any valid lookaround and then and else are any valid regexes If the lookaround succeeds, the "then" part must match for the overall regex to match. If the lookaround fails, the "else" part must match for the overall regex to match. The lookaround is zero-length. The "then" and "else" parts consume their matches like normal regexes. (?(?<=a)b|c) matches the second b and the first c in babxcac YESYESnoYESYESYESYESYESnononononononononononononono
Implicit lookahead conditional (?(regex)then|else) where regex, then, and else are any valid regexes and regex is not the name of a capturing group If "regex" is not the name of a capturing group, then it is interpreted as a lookahead as if you had written (?(?=regex)then|else). If the lookahead succeeds, the "then" part must match for the overall regex to match. If the lookahead fails, the "else" part must match for the overall regex to match. The lookaround is zero-length. The "then" and "else" parts consume their matches like normal regexes. (?(\d{2})7|c) matches the first 7 and the c in 747c noYESnononononononononononononononononononono
Named conditional (?(name)then|else) where name is the name of a capturing group and then and else are any valid regexes If the capturing group with the given name took part in the match attempt thus far, the "then" part must match for the overall regex to match. If the capturing group did not take part in the match thus far, the "else" part must match for the overall regex to match. (?<one>a)?(?(one)b|c) matches ab, the first c, and the second c in babxcac YESYESnono6.75.2.0YESYESnononoYESnononononononononono
Named conditional (?(<name>)then|else) where name is the name of a capturing group and then and else are any valid regexes If the capturing group with the given name took part in the match attempt thus far, the "then" part must match for the overall regex to match. If the capturing group did not take part in the match thus far, the "else" part must match for the overall regex to match. (?<one>a)?(?(<one>)b|c) matches ab, the first c, and the second c in babxcac nonono5.107.05.2.2YESYESnononono2.0nonononononononono
Named conditional (?('name')then|else) where name is the name of a capturing group and then and else are any valid regexes If the capturing group with the given name took part in the match attempt thus far, the "then" part must match for the overall regex to match. If the capturing group did not take part in the match thus far, the "else" part must match for the overall regex to match. (?'one'a)?(?('one')b|c) matches ab, the first c, and the second c in babxcac nonono5.107.05.2.2YESYESnononono2.0nonononononononono
Conditional (?(1)then|else) where 1 is the number of a capturing group and then and else are any valid regexes If the referenced capturing group took part in the match attempt thus far, the "then" part must match for the overall regex to match. If the capturing group did not take part in the match thus far, the "else" part must match for the overall regex to match. (a)?(?(1)b|c) matches ab, the first c, and the second c in babxcac YESYESnoYESYESYESYESYESnononoYES2.0nonononononononono
Relative conditional (?(-1)then|else) where -1 is a negative integer and then and else are any valid regexes Conditional that tests the 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 immediately before the conditional. If the referenced capturing group took part in the match attempt thus far, the "then" part must match for the overall regex to match. If the capturing group did not take part in the match thus far, the "else" part must match for the overall regex to match. (a)?(?(-1)b|c) matches ab, the first c, and the second c in babxcac nononono7.25.2.4YESYESnononononononononononononono
Forward conditional (?(+1)then|else) where +1 is a positive integer and then and else are any valid regexes Conditional that tests the 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 "then" part of conditional. If the referenced capturing group took part in the match attempt thus far, the "then" part must match for the overall regex to match. If the capturing group did not take part in the match thus far, the "else" part must match for the overall regex to match. ((?(+1)b|c)(d)?){2} matches cc and cdb in bdbdccxcdcxcdb nononono7.25.2.4YESYESnononononononononononononono
Conditional (?(+1)then|else) where 1 is the number of a capturing group and then and else are any valid regexes The + is ignored and the number is taken as an absolute reference to a capturing group. If the referenced capturing group took part in the match attempt thus far, the "then" part must match for the overall regex to match. If the capturing group did not take part in the match thus far, the "else" part must match for the overall regex to match. (a)?(?(+1)b|c) matches ab, the first c, and the second c in babxcac nononononononononononoYESnononononononononono
FeatureSyntaxDescriptionExample JGsoft .NET Java Perl PCRE PHP Delphi R JavaScript VBScript XRegExp Python Ruby std::regex 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! Credit cards, PayPal, and Bitcoin gladly accepted.

Regex Reference
Introduction
Table of Contents
Quick Reference
Characters
Basic Features
Character Classes
Anchors
Word Boundaries
Quantifiers
Unicode
Capturing Groups & Backreferences
Named Groups & Backreferences
Special Groups
Recursion & Balancing Groups
Replacement Reference
Characters
Matched Text & Backreferences
Context & Case Conversion
More on This Site
Introduction
Regular Expressions Quick Start
Regular Expressions Tutorial
Replacement Strings Tutorial
Applications and Languages
Regular Expressions Examples
Regular Expressions Reference
Replacement Strings Reference
Book Reviews
Printable PDF
About This Site
RSS Feed & Blog