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
  • (?<name>…)
  • (?P<name>…)1
  • \k<name>
  • $<name>2
  • ${name}
result.groups.name3 Sequential Error4
EcmaScript 2018
  • (?<name>…)
  • \k<name>
  • $<name>
result.groups.name Sequential Error
.NET
  • (?<name>…)
  • (?'name'…)
  • \k<name>
  • \k'name'
  • ${name}
matcher.Groups('name') Unnamed first, then named Backref to last executed participating group
Perl 5.10
  • (?<name>…)
  • (?'name'…)
  • (?P<name>…)
  • \k<name>
  • \k'name'
  • \k{name}
  • \g{name}
  • (?P=name)
  • $+{name}
$+{name} Sequential Backref to leftmost participating group
PCRE 7
  • (?<name>…)
  • (?'name'…)
  • (?P<name>…)
  • \k<name>
  • \k'name'
  • \k{name}5
  • \g{name}5
  • (?P=name)
N/A Sequential Error
PCRE 4
  • (?P<name>…)
  • (?P=name)
N/A Sequential Error
Python
  • (?P<name>…)
  • (?P=name)
  • \g<name>
result.group('name') Sequential Error
Oniguruma
  • (?<name>…)
  • (?'name'…)
  • \k<name>
  • \k'name'
  • \k<name>
  • \k'name'
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
  • (?<name>…)
  • \k<name>
  • ${name}
matcher.group('name') Sequential Error
JGsoft
  • (?<name>…)
  • (?'name'…)
  • (?P<name>…)
  • \k<name>
  • \k'name'
  • (?P=name)
  • ${name}
  • \g<name>
N/A .NET and Python styles, depending on capture syntax Same as .NET
Boost.Regex
  • (?<name>…)
  • (?'name'…)
  • \k<name>
  • \g{name}
? ? ? ?
RE2
  • (?P<name>…)
N/A ? ? ? ?
JRegex
  • ({name}…)
  • {\name}
  • ${name}
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, with the namespacing option enabled (enabled by default in XRegExp 5). Stored at result.name when namespacing is disabled.
Note: Within string.replace callbacks, stored at: arguments[arguments.length - 1].name (with namespacing enabled) or arguments[0].name (with namespacing disabled).

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).