Quick Start
Tutorial
Tools & Languages
Examples
Reference
Book Reviews
RegexBuddy—Better than a regular expression reference!

Regular Expression Reference: Capturing Groups and Backreferences

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
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 1.38–1.63
grep 1.38–1.64
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 1.38–1.64
basic 1.38–1.63
grep 1.38–1.64
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.64
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.64
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.64
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.64
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.64
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.64
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.64
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.64
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.64
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.64
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.64
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.64
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.64
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 1.38–1.64
basic 1.38–1.63
grep 1.38–1.64
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.38–1.64
basic 1.38–1.63
grep 1.38–1.64
error
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.38–1.64
basic 1.38–1.63
grep 1.38–1.64
error
errorerrorn/aerrorerrorerrorn/aerror
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! Credit cards, PayPal, and Bitcoin gladly accepted.

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