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: Named 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
Named capturing group (?<name>regex) Captures the text matched by “regex” into the group “name”. The name can contain letters and numbers but must start with a letter. (?<x>abc){3} matches abcabcabc. The group x matches abc. YESnoYESnoYESYES7YESdefault
2023/09
YES7.0YESYESYESYES1.9noECMA
1.42
nononononono
Named capturing group (?'name'regex) Captures the text matched by “regex” into the group “name”. The name can contain letters and numbers but must start with a letter. (?'x'abc){3} matches abcabcabc. The group x matches abc. YESnonononoYESnononoYES7.0YESYESYESYES1.9noECMA
1.42
nononononono
Named capturing group (?P<name>regex) Captures the text matched by “regex” into the group “name”. The name can contain letters and numbers but must start with a letter. (?P<x>abc){3} matches abcabcabc. The group x matches abc. YESYESnonoYESnononodefaultYESYESYESYESYESYESnonononononononono
Duplicate named group Any named group Two named groups can share the same name. (?<x>a)|(?<x>b) matches a or b. YESerrorerrorn/aerrorYES7 errorerrordefaultYES6.7 optionoptionoptionoptionoption1.9n/aECMA
1.42
n/an/an/an/an/an/a
Duplicate named group Any named group Named groups that share the same name are treated as one and the same group, so there are no pitfalls when using backreferences to that name. YESn/an/an/an/aYESn/an/anonononononononon/anon/an/an/an/an/an/a
Duplicate named group Any named group If a regex has multiple groups with the same name, backreferences using that name point to the leftmost group in the regex with that name. non/an/an/an/anon/an/adefaultno6.7–8.33no5.5.0–5.5.9XE–XE62.14.0–3.0.2non/anon/an/an/an/an/an/a
Duplicate named group Any named group If a regex has multiple groups with the same name, backreferences using that name point to the leftmost group with that name that has actually participated in the match attempt when the backreference is evaluated. non/an/an/an/anon/an/anoYES8.36YES5.6.910.23.1.3non/aECMA
1.47
n/an/an/an/an/an/a
Duplicate named group Any named group If a regex has multiple groups with the same name, backreferences using that name point to the rightmost group with that name that appears to the left of the backreference in the regex. non/an/an/an/anon/an/anonononononononon/aECMA
1.42–1.46
n/an/an/an/an/an/a
Duplicate named group Any named group If a regex has multiple groups with the same name, backreferences using that name can match the text captured by any group with that name that appears to the left of the backreference in the regex. non/an/an/an/anon/an/anonononononono1.9n/anon/an/an/an/an/an/a
Duplicate named group Any named group If a regex has multiple groups with the same name, the regex engine returns the text matched by the last group with that name to participate in the match. non/an/an/an/anon/an/anonononoYESYESno1.9n/anon/an/an/an/an/an/a
Named backreference \k<name> Substituted with the text matched by the named group “name”. (?<x>abc|def)=\k<x> matches abc=abc or def=def, but not abc=def or def=abc. YESnoYESnoYESYES7YESnoYES7.0YESYESYESYES1.9noECMA
1.47
nononononono
Named backreference \k'name' Substituted with the text matched by the named group “name”. (?'x'abc|def)=\k'x' matches abc=abc or def=def, but not abc=def or def=abc. YESnonononoYESnononoYES7.0YESYESYESYES1.9noECMA
1.47
nononononono
Named backreference \k{name} Substituted with the text matched by the named group “name”. (?'x'abc|def)=\k{x} matches abc=abc or def=def, but not abc=def or def=abc. nononononononononoYES7.2YESYESYESYESnonoECMA
1.47
nononononono
Named backreference \g{name} Substituted with the text matched by the named group “name”. (?'x'abc|def)=\g{x} matches abc=abc or def=def, but not abc=def or def=abc. nononononononononoYES7.2YESYESYESYESnonoECMA
1.42
nononononono
Named backreference (?P=name) Substituted with the text matched by the named group “name”. (?P<x>abc|def)=(?P=x) matches abc=abc or def=def, but not abc=def or def=abc. YESYESnononononononoYESYESYESYESYESYESnonononononononono
Failed backreference Any named backreference Backreferences to groups that did not participate in the match attempt fail to match. (?<x>a)?\k<x> matches aa but fails to match b. YESYESignoredn/aignorednon‑ECMA7YESn/aYESYESYESYESYESYES1.9n/aECMA
1.47
n/an/an/an/an/an/a
Nested backreference Any named backreference Backreferences can be used inside the group they reference. (?<x>a\k<x>?){3} matches aaaaaa. YES2.4–3.4 failignoredn/aignoredYES7YESn/aYES6.5
4.5 error
YESYESYESYES1.9 failn/aECMA
1.78 fail
n/an/an/an/an/an/a
Forward reference Any named backreference Backreferences can be used before the group they reference. (\k<x>?(?<x>a)){3} matches aaaaaa. YESerrorignoredn/aerrorYES7 errorerrorn/aYES6.7
4.5 error
YESYESYESYES1.9 errorn/aECMA
1.42 error
n/an/an/an/an/an/a
Named capturing group Any named capturing group A number is a valid name for a capturing group. (?<17>abc){3} matches abcabcabc. The group named “17” matches abc. YESerrorerrorn/aerrorYES7 errorerrordefaulterror4.5–8.33errorerrorXE–XE62.14.0–3.0.21.9 errorn/aECMA
1.42
n/an/an/an/an/an/a
Named backreference Any named backreference A number is a valid name for a backreference which then points to a group with that number as its name. (?<17>abc|def)=\k<17> matches abc=abc or def=def, but not abc=def or def=abc. YESn/an/an/an/aYESn/an/adefault errorn/a4.5–8.33n/an/aXE–XE62.14.0–3.0.2n/an/aECMA
1.42 error
n/an/an/an/an/an/a
Named capturing group Any capturing group with a number as its name If the name of the group is a number, that becomes the group’s name and the group’s number. (?<17>abc|def)=\17 matches abc=abc or def=def, but not abc=def or def=abc. non/an/an/an/aYESn/an/anon/anon/an/anonon/an/anon/an/an/an/an/an/a
Named capturing group Any named capturing group A negative number is a valid name for a capturing group. (?<-17>abc){3} matches abcabcabc. The group named “-17” matches abc. errorerrorerrorn/aerrorerror7 errorerrordefault errorerrorerrorerrorerrorerrorerror1.9 errorn/aECMA
1.42
n/an/an/an/an/an/a
Named backreference Any named backreference A negative number is a valid name for a backreference which then points to a group with that negative number as its name. n/an/an/an/an/an/an/an/an/an/an/an/an/an/an/an/an/aECMA
1.42 error
n/an/an/an/an/an/a
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

Ad-Free Access and Printable PDF Download

If you find the content on this website helpful they you may want a copy you can read offline or even print, or browse the site as often as you want without ads. You can purchase your own copy of the Regular-Expressions.info printable PDF download. As a bonus, you'll get a lifetime of advertisement-free access to this site!