Quick Start
Tutorial
Tools & Languages
Examples
Reference
Book Reviews
Regex Reference
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
Replacement Reference
Characters
Matched Text & Backreferences
Context & Case Conversion
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: Capturing Groups and Backreferences

FeatureSyntaxDescriptionExampleJGsoft .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
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. YESYESYESYESYESYESYESYESYESYESYESYESYESYESECMA
extended
egrep
awk
ECMA
extended
egrep
awk
YESnoYESnoYESYESYESYES
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. nonononononononononononononobasic
grep
basic
grep
noYESnoYESnononono
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. YESYESYESYESYESYESYESYESYESYESYESYESYESYESECMAECMAYESnonononononoYES
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. YESYESYESYESYESYESYESYESYESYESYESYESYESYESECMA
basic
grep
ECMA
basic
grep
YESYESnoYESYESYESnoYES
Backreference \10 through \99 Substituted with the text matched between the 10th through 99th numbered capturing group. YESYESYESYESYESYESYESYESYESYESYESYESYESYESECMAnoYESnonononononoYES
Backreference \k<1> through \k<99> Substituted with the text matched between the 1st through 99th numbered capturing group. (abc|def)=\k<1> matches abc=abc or def=def, but not abc=def or def=abc. YESYESnononononononononoYESno1.9noECMA
1.47–1.85
nononononononono
Backreference \k'1' through \k'99' Substituted with the text matched between the 1st through 99th numbered capturing group. (abc|def)=\k'1' matches abc=abc or def=def, but not abc=def or def=abc. YESYESnonononononononononono1.9noECMA
1.47–1.85
nononononononono
Backreference \g1 through \g99 Substituted with the text matched between the 1st through 99th numbered capturing group. (abc|def)=\g1 matches abc=abc or def=def, but not abc=def or def=abc. nonono5.107.0YES5.2.2YESYESnonononononoECMA
1.42–1.85
nononononononono
Backreference \g{1} through \g{99} Substituted with the text matched between the 1st through 99th numbered capturing group. (abc|def)=\g{1} matches abc=abc or def=def, but not abc=def or def=abc. nonono5.107.0YES5.2.2YESYESnonononononoECMA
1.42–1.85
nononononononono
Backreference \g<1> through \g<99> Substituted with the text matched between the 1st through 99th numbered capturing group. (abc|def)=\g<1> matches abc=abc or def=def, but not abc=def or def=abc. nononononononononononononononoECMA
1.47–1.85
nononononononono
Backreference \g'1' through \g'99' Substituted with the text matched between the 1st through 99th numbered capturing group. (abc|def)=\g'1' matches abc=abc or def=def, but not abc=def or def=abc. nononononononononononononononoECMA
1.47–1.85
nononononononono
Backreference (?P=1) through (?P=99) Substituted with the text matched between the 1st through 99th numbered capturing group. (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. V2nononononononononononono1.9noECMA
1.47–1.85
nononononononono
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. V2nononononononononononono1.9noECMA
1.47–1.85
nononononononono
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. nonono5.107.0YES5.2.2YESYESnonononononoECMA
1.42–1.85
nononononononono
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. nonono5.107.0YES5.2.2YESYESnonononononoECMA
1.42–1.85
nononononononono
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. nononononononononononononononoECMA
1.47–1.85
nononononononono
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. nononononononononononononononoECMA
1.47–1.85
nononononononono
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. YESnon‑ECMAYESYESYESYESYESYESYESignoredignoredignoredYESYESECMA
basic
grep
ignored
ECMA
1.47–1.85
YESYESn/aYESYESYESn/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. errorerrorYESerrorerrorerrorerrorerrorerrorerrorerrorerrorerror1.8 onlyECMA
basic
grep
error
ECMA
basic
grep
error
errorerrorn/aerrorerrorerrorn/aerror
Nested backreference Any numbered backreference Backreferences can be used inside the group they reference. (a\1?){3} matches aaaaaa. YESYESYESYESYESYESYESYESYESignoredYESignorederrorfailECMA
basic
grep
error
ECMA
1.78–1.85 fail
errorerrorn/aerrorerrorerrorn/aerror
Forward reference Any numbered backreference Backreferences can be used before the group they reference. (\2?(a)){3} matches aaaaaa. YESnon‑ECMAYESYESYESYESYESYESYESignorederrorerrorerrorYESECMA
basic
grep
error
ECMA
1.78–1.85
errorerrorn/aerrorerrorerrorn/aerror
FeatureSyntaxDescriptionExampleJGsoft .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