New syntax » Named capture comparison
There are several different syntaxes used for named capture. Although Python was the first to implement the feature, most libraries have adopted .NET's alternative syntax.
Library | Capture | Backref in regex | Backref in replacement | Stored at | Backref numbering | Multiple groups with same name |
---|---|---|---|---|---|---|
XRegExp |
|
|
|
result.groups.name 3
|
Sequential | Error4 |
EcmaScript 2018 |
|
|
|
result.groups.name
|
Sequential | Error |
.NET |
|
|
|
matcher.Groups('name') |
Unnamed first, then named | Backref to last executed participating group |
Perl 5.10 |
|
|
|
$+{name} |
Sequential | Backref to leftmost participating group |
PCRE 7 |
|
|
N/A | Sequential | Error | |
PCRE 4 |
|
|
N/A | Sequential | Error | |
Python |
|
|
|
result.group('name') |
Sequential | Error |
Oniguruma |
|
|
|
N/A | Unnamed groups default to noncapturing when mixed with named groups | Backref to rightmost participating group. Backrefs within a regex work as alternation of matches of all preceding groups with the same name, in reverse order. |
Java 7 |
|
|
|
matcher.group('name') |
Sequential | Error |
JGsoft |
|
|
|
N/A | .NET and Python styles, depending on capture syntax | Same as .NET |
Boost.Regex |
|
|
? | ? | ? | ? |
RE2 |
|
N/A | ? | ? | ? | ? |
JRegex |
|
|
|
matcher.group('name') |
? | ? |
1 As of XRegExp 2. Not recommended for use, because support for the (?P<name>…)
syntax may be removed in future versions of XRegExp. It is currently supported only to avoid an octal escape versus backreference issue in old Opera. Opera supported the Python named capture syntax natively, but did not provide full named capture functionality.
2 As of XRegExp 4.
3 As of XRegExp 4.1, when the namespacing
option is on (it's on by default in XRegExp 5). Stored at result.name
when namespacing
is off.
Note: Within string.replace
callbacks, stored at: arguments[arguments.length - 1].name
(with namespacing
on) or arguments[0].name
(with namespacing
off).
4 As of XRegExp 3.
5 As of PCRE 7.2.
TODO: Add a column comparing the use of capture names in regex conditionals (not supported by XRegExp).