Replacement Reference |
Characters |
Matched Text & Backreferences |
Context & Case Conversion |
Conditionals |
Feature | Syntax | Description | Example | JGsoft | .NET | Java | Perl | PCRE | PCRE2 | PHP | Delphi | R | JavaScript | VBScript | XRegExp | Python | Ruby | std::regex | Boost | 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. | YES | YES | YES | YES | YES | YES | YES | YES | YES | YES | YES | YES | YES | YES | ECMA extended egrep awk | ECMA extended egrep awk | YES | no | YES | no | YES | YES | YES | YES |
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. | no | no | no | no | no | no | no | no | no | no | no | no | no | no | basic grep | basic grep | no | YES | no | YES | no | no | no | no |
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. | YES | YES | YES | YES | YES | YES | YES | YES | YES | YES | YES | YES | YES | YES | ECMA | ECMA | YES | no | no | no | no | no | no | YES |
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. | YES | YES | YES | YES | YES | YES | YES | YES | YES | YES | YES | YES | YES | YES | ECMA basic grep | ECMA basic grep | YES | YES | no | YES | YES | YES | no | YES |
Backreference | \10 through \99 | Substituted with the text matched between the 10th through 99th numbered capturing group. | YES | YES | YES | YES | YES | YES | YES | YES | YES | YES | YES | YES | YES | YES | ECMA | no | YES | no | no | no | no | no | no | YES | |
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. | YES | YES | no | no | no | no | no | no | no | no | no | YES | no | 1.9 | no | ECMA 1.47–1.85 | no | no | no | no | no | no | no | no |
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. | YES | YES | no | no | no | no | no | no | no | no | no | no | no | 1.9 | no | ECMA 1.47–1.85 | no | no | no | no | no | no | no | no |
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. | no | no | no | 5.10 | 7.0 | YES | 5.2.2 | YES | YES | no | no | no | no | no | no | ECMA 1.42–1.85 | no | no | no | no | no | no | no | no |
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. | no | no | no | 5.10 | 7.0 | YES | 5.2.2 | YES | YES | no | no | no | no | no | no | ECMA 1.42–1.85 | no | no | no | no | no | no | no | no |
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. | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no | ECMA 1.47–1.85 | no | no | no | no | no | no | no | no |
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. | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no | ECMA 1.47–1.85 | no | no | no | no | no | no | no | no |
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. | YES | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no |
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. | V2 | no | no | no | no | no | no | no | no | no | no | no | no | 1.9 | no | ECMA 1.47–1.85 | no | no | no | no | no | no | no | no |
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. | V2 | no | no | no | no | no | no | no | no | no | no | no | no | 1.9 | no | ECMA 1.47–1.85 | no | no | no | no | no | no | no | no |
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. | no | no | no | 5.10 | 7.0 | YES | 5.2.2 | YES | YES | no | no | no | no | no | no | ECMA 1.42–1.85 | no | no | no | no | no | no | no | no |
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. | no | no | no | 5.10 | 7.0 | YES | 5.2.2 | YES | YES | no | no | no | no | no | no | ECMA 1.42–1.85 | no | no | no | no | no | no | no | no |
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. | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no | ECMA 1.47–1.85 | no | no | no | no | no | no | no | no |
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. | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no | ECMA 1.47–1.85 | no | no | no | no | no | no | no | no |
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. | YES | non‑ECMA | YES | YES | YES | YES | YES | YES | YES | ignored | ignored | ignored | YES | YES | ECMA basic grep ignored | ECMA 1.47–1.85 | YES | YES | n/a | YES | YES | YES | n/a | ignored |
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. | error | error | YES | error | error | error | error | error | error | error | error | error | error | 1.8 only | ECMA basic grep error | ECMA basic grep error | error | error | n/a | error | error | error | n/a | error |
Nested backreference | Any numbered backreference | Backreferences can be used inside the group they reference. | (a\1?){3} matches aaaaaa. | YES | YES | YES | YES | YES | YES | YES | YES | YES | ignored | YES | ignored | error | fail | ECMA basic grep error | ECMA 1.78–1.85 fail | error | error | n/a | error | error | error | n/a | error |
Forward reference | Any numbered backreference | Backreferences can be used before the group they reference. | (\2?(a)){3} matches aaaaaa. | YES | non‑ECMA | YES | YES | YES | YES | YES | YES | YES | ignored | error | error | error | YES | ECMA basic grep error | ECMA 1.78–1.85 | error | error | n/a | error | error | error | n/a | error |
Feature | Syntax | Description | Example | JGsoft | .NET | Java | Perl | PCRE | PCRE2 | PHP | Delphi | R | JavaScript | VBScript | XRegExp | Python | Ruby | std::regex | Boost | Tcl ARE | POSIX BRE | POSIX ERE | GNU BRE | GNU ERE | Oracle | XML | XPath |
| Quick Start | Tutorial | Tools & Languages | Examples | Reference | Book Reviews |
| Introduction | Table of Contents | Quick Reference | Characters | Basic Features | Character Classes | Shorthands | Anchors | Word Boundaries | Quantifiers | Unicode | Capturing Groups & Backreferences | Named Groups & Backreferences | Special Groups | Mode Modifiers | Recursion & Balancing Groups |
| Characters | Matched Text & Backreferences | Context & Case Conversion | Conditionals |
Page URL: https://www.regular-expressions.info/refcapture.html
Page last updated: 16 August 2024
Site last updated: 29 August 2024
Copyright © 2003-2024 Jan Goyvaerts. All rights reserved.