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

FeatureSyntaxDescriptionExample JGsoft .NET Java Perl PCRE PHP Delphi R JavaScript VBScript XRegExp Python Ruby std::regex Tcl ARE POSIX BRE POSIX ERE GNU BRE GNU ERE 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. YESYES75.107.05.2.2YESYESnonoYESno1.9nonononononononono
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. YESYESno5.107.05.2.2YESYESnononono1.9nonononononononono
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. YESnono5.10YESYESYESYESnonoYESYESnononononononononono
Duplicate named group Any named group Two named groups can share the same name. (?<x>a)|(?<x>b) matches a or b. YESYES7 error5.106.7 option5.2.0 optionoptionoptionn/an/aerrorerror1.9n/an/an/an/an/an/an/an/an/a
Duplicate named group Any named group Two named groups that share the same name are treated as one an the same group, so there are no pitfalls when using backreferences to that name. YESYESn/anononononon/an/an/an/anon/an/an/an/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. YESYES75.107.05.2.2YESYESnonoYESno1.9nonononononononono
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. YESYESno5.107.05.2.2YESYESnononono1.9nonononononononono
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. nonono5.107.25.2.4YESYESnononononononononononononono
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. nonono5.107.25.2.4YESYESnononononononononononononono
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. YESnono5.10YESYESYESYESnononoYESnononononononononono
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. YESYES75.10YESYESYESYESn/an/aignoredYES1.9n/an/an/an/an/an/an/an/an/a
Invalid backreference Any named backreference Backreferences to groups that do not exist at all are valid but fail to match anything. (a)?\k<y>|b matches b in aab. errorerror75.10 errorerrorerrorerrorerrorn/an/aignorederror1.9 errorn/an/an/an/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. YESYES75.106.55.1.3YESYESn/an/aignoredfail1.9 failn/an/an/an/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. YESYES7 error5.106.75.2.0YESYESn/an/aerrorerror1.9 errorn/an/an/an/an/an/an/an/an/a
FeatureSyntaxDescriptionExample JGsoft .NET Java Perl PCRE 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!

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
Recursion & Balancing Groups
Replacement Reference
Characters
Matched Text & Backreferences
Context & Case Conversion
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