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: Capturing Groups and Backreferences

FeatureSyntaxDescriptionExample JGsoft .NET Java Perl PCRE PCRE2 PHP Delphi R JavaScript VBScript XRegExp Python Ruby std::regex 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
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
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. YESYESYESYESYESYESYESYESYESYESYESYESYESYESECMAYESnonononononono
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
YESYESnoYESYESYESnoYES
Backreference \10 through \99 Substituted with the text matched between the 10th through 99th numbered capturing group. YESYESYESYESYESYESYESYESYESYESYESYESYESYESECMAYESnonononononoYES
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.9nonononononononono
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.9nonononononononono
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.2YESYESnononononononononononononono
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.2YESYESnononononononononononononono
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. YESnononononononononononononononononononononono
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.9nonononononononono
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.9nonononononononono
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.2YESYESnononononononononononononono
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.2YESYESnononononononononononononono
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
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
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
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
errorerrorn/aerrorerrorerrorn/aerror
FeatureSyntaxDescriptionExample JGsoft .NET Java Perl PCRE PCRE2 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
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