Quick Start
Tutorial
Search & Replace
Tools & Languages
Examples
Reference
Regex Reference
Introduction
Table of Contents
Quick Reference
Characters
Basic Features
Character Classes
Shorthands
Anchors
Word Boundaries
Quantifiers
Capturing Groups & Backreferences
Named Groups & Backreferences
Special Groups
Unicode Characters and Properties
Unicode Versions
Unicode Categories
Unicode Scripts
Unicode Blocks
Unicode Binary Properties
Unicode Property Sets
Unicode Boundaries
Mode Modifiers
Recursion & Balancing Groups
Backtracking Control Verbs
Replacement Reference
Characters
Matched Text & Backreferences
Case Conversion
Context
Conditionals
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
RegexBuddy—Better than a regular expression reference!

Regular Expression Reference: Groups and Backreferences

FeatureSyntaxDescriptionExampleJGsoft Python JavaScript VBScript XRegExp .NET Java ICU RE2 Perl PCRE PCRE2 PHP Delphi R Ruby std::regex Boost Tcl POSIX GNU Oracle XML XPath
Capturing group (regex) Parentheses group the regex between them. They capture the text matched by the regex inside them into a numbered group that can be reused with a numbered backreference. They allow you to apply regex operators to the entire grouped regex. (abc){3} matches abcabcabc. First group matches abc. YESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESECMA
extended
egrep
awk
ECMA
extended
egrep
awk
YESextendedextendedYESYESYES
Capturing group \(regex\) Escaped parentheses group the regex between them. They capture the text matched by the regex inside them into a numbered group that can be reused with a numbered backreference. They allow you to apply regex operators to the entire grouped regex. \(abc\){3} matches abcabcabc. First group matches abc. nonononononononononononononononobasic
grep
basic
grep
nobasicbasicnonono
Capturing group 100 or more capturing groups supported in a single regular expression. YES3.5YESYESYESYESYESYESYESYESYESYESYESnoYESYESVC’12YESYESYESYESYESYESYES
Non-capturing group (?:regex) Non-capturing parentheses group the regex so you can apply regex operators, but do not capture anything. (?:abc){3} matches abcabcabc. No groups. YESYESYESYESYESYESYESYESdefaultYESYESYESYESYESYESYESECMAECMAYESnononono3
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 V2nonononononononoYES7.2YESYESYESYESnonoECMA
1.42
nononononono
Atomic group (?>regex) Atomic groups prevent the regex engine from backtracking into the group after a match has been found for the group. If the remainder of the regex fails then the engine may backtrack over the group if a quantifier or alternation makes it optional or backtrack to something prior to the group. But it will not backtrack into the group to try other permutations of the group. a(?>bc|b)c matches abcc but not abc YES3.11nononoYESYESYESnoYESYESYESYESYESYESYESnoECMAnononononono
Atomic group (*atomic:regex) Alternate syntax for the above. a(*atomic:bc|b)c matches abcc but not abc nonononononononono5.28no10.337.4.0no4.0.0nonononononononono
Comment (?#comment) Everything between (?# and ) is ignored by the regex engine. a(?#foobar)b matches ab YESYESnonoYESYESnoYESnoYESYESYESYESYESYESYESnoECMAYESnonononono
Backreference \1 through \9 Substituted with the text matched between the 1st through 9th numbered capturing group. (abc|def)=\1 matches abc=abc or def=def, but not abc=def or def=abc. YESYESYESYESYESYESYESYESnoYESYESYESYESYESYESYESECMA
basic
grep
ECMA
basic
grep
YESbasicYESYESnoYES
Backreference \10 through \99 Substituted with the text matched between the 10th through 99th numbered capturing group. YESYESYESYESYESYESYESYESnoYESYESYESYESYESYESYESECMAnoYESnonononoYES
Backreference \10 through \99 Substituted with the text matched between the 1st through 9th numbered capturing group followed by a literal digit if the regex has less than 10 capturing groups. (a)\17 matches aa7. YESnonoYESnoECMAYESYESnononononononononoECMA
basic
grep
nobasicYESYESnoYES
Backreference \100 through \999 Substituted with the text matched between the 100th through 999th numbered capturing group. nonoYESnoYESYESYESYESnoYESYESYESYESnoYESYESECMA
VC’12
noYESnonononoYES
Backreference \100 through \999 Substituted with the text matched between the 10th through 99th numbered capturing group followed by a literal digit if the regex has less than 100 capturing groups. YESnonononoECMAYESYESnononononononononononononononoYES
Backreference \k<1>, \k<2>, etc. Substituted with the text matched by the capturing group with the specified number. (abc|def)=\k<1> matches abc=abc or def=def, but not abc=def or def=abc. YESnononoYESYESnonononononononono1.9noECMA
1.47
nononononono
Backreference \k'1', \k'2', etc. Substituted with the text matched by the capturing group with the specified number. (abc|def)=\k'1' matches abc=abc or def=def, but not abc=def or def=abc. YESnonononoYESnonononononononono1.9noECMA
1.47
nononononono
Backreference \g1, \g2, etc. Substituted with the text matched by the capturing group with the specified number. (abc|def)=\g1 matches abc=abc or def=def, but not abc=def or def=abc. nononononononononoYES7.0YESYESYESYESnonoECMA
1.42
nononononono
Backreference \g{1}, \g{2}, etc. Substituted with the text matched by the capturing group with the specified number. (abc|def)=\g{1} matches abc=abc or def=def, but not abc=def or def=abc. nononononononononoYES7.0YESYESYESYESnonoECMA
1.42
nononononono
Backreference \g<1>, \g<2>, etc. Substituted with the text matched by the capturing group with the specified number. (abc|def)=\g<1> matches abc=abc or def=def, but not abc=def or def=abc. nononononononononononononononononoECMA
1.47
nononononono
Backreference \g'1', \g'2', etc. Substituted with the text matched by the capturing group with the specified number. (abc|def)=\g'1' matches abc=abc or def=def, but not abc=def or def=abc. nononononononononononononononononoECMA
1.47
nononononono
Backreference (?P=1), (?P=2), etc. Substituted with the text matched by the capturing group with the specified number. (abc|def)=(?P=1) matches abc=abc or def=def, but not abc=def or def=abc. YESnonononononononononononononononononononononono
Relative Backreference \k<-1>, \k<-2>, etc. Substituted with the text matched by 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 at the backreference. (a)(b)(c)(d)\k<-3> matches abcdb. V2nononononononononononononono1.9noECMA
1.47
nononononono
Relative Backreference \k'-1', \k'-2', etc. Substituted with the text matched by 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 at the backreference. (a)(b)(c)(d)\k'-3' matches abcdb. V2nononononononononononononono1.9noECMA
1.47
nononononono
Relative Backreference \g-1, \g-2, etc. Substituted with the text matched by 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 at the backreference. (a)(b)(c)(d)\g-3 matches abcdb. nononononononononoYES7.0YESYESYESYESnonoECMA
1.42
nononononono
Relative Backreference \g{-1}, \g{-2}, etc. Substituted with the text matched by 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 at the backreference. (a)(b)(c)(d)\g{-3} matches abcdb. nononononononononoYES7.0YESYESYESYESnonoECMA
1.42
nononononono
Relative Backreference \g<-1>, \g<-2>, etc. Substituted with the text matched by 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 at the backreference. (a)(b)(c)(d)\g<-3> matches abcdb. nononononononononononononononononoECMA
1.47
nononononono
Relative Backreference \g'-1', \g'-2', etc. Substituted with the text matched by 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 at the backreference. (a)(b)(c)(d)\g'-3' matches abcdb. nononononononononononononononononoECMA
1.47
nononononono
Failed backreference Any numbered backreference Backreferences to groups that did not participate in the match attempt fail to match. (a)?\1 matches aa but fails to match b. YESYESignoredignoredignorednon‑ECMAYESYESn/aYESYESYESYESYESYESYESECMA
basic
grep
ignored
ECMA
1.47
YESbasicYESYESn/aignored
Invalid backreference Any numbered backreference Backreferences to groups that do not exist at all are valid but fail to match anything. (a)?\2|b matches b in aab. errorerrorerrorerrorerrorerrorYESerrorn/aerrorerrorerrorerrorerrorerror1.8 onlyECMA
basic
grep
error
ECMA
basic
grep
error
errorbasic errorerrorerrorn/aerror
Nested backreference Any numbered backreference Backreferences can be used inside the group they reference. (a\1?){3} matches aaaaaa. YESerrorignoredYESignoredYESYESYESn/aYESYESYESYESYESYESfailECMA
basic
grep
error
ECMA
1.78 fail
errorbasic errorerrorYESn/aignored
Forward reference Any numbered backreference Backreferences can be used before the group they reference. (\2?(a)){3} matches aaaaaa. YESerrorignorederrorerrornon‑ECMAYESYESn/aYESYESYESYESYESYESYESECMA
basic
grep
error
ECMA
1.78
errorbasic errorerrorerrorn/aerror
FeatureSyntaxDescriptionExampleJGsoft Python JavaScript VBScript XRegExp .NET Java ICU RE2 Perl PCRE PCRE2 PHP Delphi R Ruby std::regex Boost Tcl POSIX GNU Oracle XML XPath