Replacement Reference |
Introduction |
Table of Contents |
Characters |
Matched Text & Backreferences |
Context & Case Conversion |
Conditionals |
Regular Expressions Reference |
Feature | Syntax | Description | Example | JGsoft | .NET | Java | Perl | PCRE2 | PHP | Delphi | R | JavaScript | VBScript | XRegExp | Python | Ruby | std::regex | Boost | Tcl ARE | Oracle | XPath |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Ampersand | \& | Insert a literal ampersand. | Replacing with \& yields & | no | no | YES | YES | extended | no | no | YES | no | no | no | no | no | sed | YES | YES | no | error |
Whole match | \& | Insert the whole regex match. | Replacing \d+ with [\&] in 1a2b yields [1]a[2]b | YES | no | no | no | no | no | YES | no | no | no | no | no | YES | no | no | no | no | error |
Whole match | $& | Insert the whole regex match. | Replacing \d+ with [$&] in 1a2b yields [1]a[2]b | YES | YES | error | YES | error | no | YES | no | YES | YES | YES | no | no | default | all default | no | no | error |
Whole match | & | Insert the whole regex match. | Replacing \d+ with [&] in 1a2b yields [1]a[2]b | no | no | no | no | no | no | no | no | no | no | no | no | no | sed | sed | YES | no | no |
Whole match | \0 | Insert the whole regex match. | Replacing \d+ with [\0] in 1a2b yields [1]a[2]b | YES | no | no | no | no | YES | YES | no | no | no | no | no | YES | sed | sed | YES | no | error |
Whole match | $0 | Insert the whole regex match. | Replacing \d+ with [$0] in 1a2b yields [1]a[2]b | YES | YES | YES | error | YES | YES | YES | no | no | no | YES | no | no | default VC’08–VC’13 | all default | no | no | YES |
Whole match | \g<0> | Insert the whole regex match. | Replacing \d+ with [\g<0>] in 1a2b yields [1]a[2]b | YES | no | no | no | no | no | no | no | no | no | no | YES | no | no | no | no | no | error |
Whole match | $MATCH and ${^MATCH} | Insert the whole regex match. | Replacing \d+ with [$MATCH] in 1a2b yields [1]a[2]b | no | no | error | error | error | no | no | no | no | no | error | no | no | no | all default 1.42–1.85 | no | no | error |
Backreference | \1 through \9 | Insert the text matched by one of the first 9 capturing groups. | Replacing (a)(b)(c) with \3\3\1 in abc yields cca | YES | no | no | YES | no | YES | YES | YES | no | no | no | YES | YES | sed | YES | YES | YES | error |
Backreference | \10 through \99 | Insert the text matched by capturing groups 10 through 99. | YES | no | no | no | no | YES | YES | no | no | no | no | YES | no | no | no | no | no | no | |
Backreference and literal | \10 through \99 | When there are fewer capturing groups than the 2-digit number, treat this as a single-digit backreference followed by a literal number instead of as an invalid backreference. | Replacing (a)(b)(c) with \39\38\17 in abc yields c9c8a7 | YES | n/a | n/a | n/a | n/a | no | YES | n/a | n/a | n/a | n/a | no | n/a | n/a | n/a | n/a | n/a | n/a |
Backreference | $1 through $9 | Insert the text matched by one of the first 9 capturing groups. | Replacing (a)(b)(c) with $3$3$1 in abc yields cca | YES | YES | YES | YES | YES | YES | YES | no | YES | YES | YES | no | no | default | all default | no | no | YES |
Backreference | $10 through $99 | Insert the text matched by capturing groups 10 through 99. | YES | YES | YES | YES | YES | YES | YES | no | YES | YES | YES | no | no | default | all default | no | no | YES | |
Backreference and literal | $10 through $99 | When there are fewer capturing groups than the 2-digit number, treat this as a single-digit backreference followed by a literal number instead of as an invalid backreference. | Replacing (a)(b)(c) with $39$38$17 in abc yields c9c8a7 | YES | ECMA | YES | no | no | no | YES | n/a | YES | YES | no | n/a | n/a | no | no | n/a | n/a | YES |
Backreference | ${1} through ${99} | Insert the text matched by capturing groups 1 through 99. | Replacing (a)(b)(c) with ${3}${3}${1} in abc yields cca | YES | YES | error | YES | YES | YES | YES | no | no | no | YES | no | no | no | all default | no | no | error |
Backreference | \g<1> through \g<99> | Insert the text matched by capturing groups 1 through 99. | Replacing (a)(b)(c) with \g<3>\g<3>\g<1> in abc yields cca | YES | no | no | no | no | no | no | no | no | no | no | YES | no | no | no | no | no | error |
Named backreference | ${name} | Insert the text matched by the named capturing group “name”. | Replacing (?'one'a)(?'two'b) with ${two}${one} in ab yields ba | YES | YES | 7 | error | YES | no | YES | no | no | no | YES | no | no | no | no | no | no | error |
Named backreference | $+{name} | Insert the text matched by the named capturing group “name”. | Replacing (?'one'a)(?'two'b) with $+{two}$+{one} in ab yields ba | no | no | error | 5.10 | error | no | no | no | no | no | error | no | no | no | all default 1.42–1.85 | no | no | error |
Named backreference | $name | Insert the text matched by the named capturing group “name”. | Replacing (?'one'a)(?'two'b) with $two$one in ab yields ba | no | no | error | error | YES | no | no | no | no | no | error | no | no | no | no | no | no | error |
Named backreference | \g<name> | Insert the text matched by the named capturing group “name”. | Replacing (?P<one>a)(?P<two>b) with \g<two>\g<one> in ab yields ba | YES | no | no | no | no | no | YES | no | no | no | no | YES | no | no | no | no | no | error |
Invalid backreference | Any supported backreference syntax | A backreference that indicates a number greater than the highest-numbered group or a name of a group that does not exist is replaced with the empty string. | V1 only | no | error | YES | error | YES | YES | YES | no | no | error | error | YES | default VC’08–VC’22 sed VC’15–VC’22 | YES | YES | YES | YES | |
Invalid backreference | Any supported backreference syntax | A backreference that indicates a number greater than the highest-numbered group or a name of a group that does not exist is treated as literal text that is inserted as such in the replacement. | V2 error | YES | error | no | error | no | no | no | YES | YES | error | error | no | sed VC’08–VC’13 error | no | no | no | no | |
Backreference to non-participating group | Any supported backreference syntax | A backreference to a non-participating capturing group is replaced with the empty string. | YES | YES | YES | YES | error | YES | YES | YES | YES | YES | YES | 3.5 | YES | YES | YES | YES | YES | YES | |
Last backreference | \+ | Insert the text matched by the highest-numbered capturing group that actually participated in the match. | Replacing (a)(z)? with [\+] in ab yields [a]b | YES | no | no | no | no | no | YES | no | no | no | no | no | YES | no | no | no | no | error |
Last backreference | \+ | Insert the text matched by the highest-numbered capturing group, regardless of whether it participated in the match. | Replacing (a)(z)? with [\+] in ab yields []b | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no | error |
Last backreference | $+ | Insert the text matched by the highest-numbered capturing group that actually participated in the match. | Replacing (a)(z)? with [$+] in ab yields [a]b | YES | no | error | 5.18 | error | no | YES | no | no | no | error | no | no | no | no | no | no | error |
Last backreference | $+ | Insert the text matched by the highest-numbered capturing group, regardless of whether it participated in the match. | Replacing (a)(z)? with [$+] in ab yields []b | no | YES | error | 5.8–5.16 | error | no | no | no | no | YES | error | no | no | no | all default 1.42–1.85 | no | no | error |
Last backreference | $^N | Insert the text matched by the highest-numbered capturing group that actually participated in the match. | Replacing (a)(z)? with [$^N] in ab yields [a]b | no | no | error | YES | error | no | no | no | no | no | error | no | no | no | all default 1.42–1.85 | no | no | error |
Last backreference | $LAST_SUBMATCH_RESULT and ${^LAST_SUBMATCH_RESULT} | Insert the text matched by the highest-numbered capturing group that actually participated in the match. | Replacing (a)(z)? with [$LAST_SUBMATCH_RESULT] in ab yields [a]b | no | no | error | error | error | no | no | no | no | no | error | no | no | no | all default 1.42–1.85 | no | no | error |
Last backreference | $LAST_PAREN_MATCH and ${^LAST_PAREN_MATCH} | Insert the text matched by the highest-numbered capturing group, regardless of whether it participated in the match. | Replacing (a)(z)? with [$LAST_PAREN_MATCH] in ab yields []b | no | no | error | error | error | no | no | no | no | no | error | no | no | no | all default 1.42–1.85 | no | no | error |
Feature | Syntax | Description | Example | JGsoft | .NET | Java | Perl | PCRE2 | PHP | Delphi | R | JavaScript | VBScript | XRegExp | Python | Ruby | std::regex | Boost | Tcl ARE | Oracle | XPath |
| Quick Start | Tutorial | Tools & Languages | Examples | Reference | Book Reviews |
| Introduction | Table of Contents | Characters | Matched Text & Backreferences | Context & Case Conversion | Conditionals | Regular Expressions Reference |
Page URL: https://www.regular-expressions.info/refreplacebackref.html
Page last updated: 16 August 2024
Site last updated: 06 November 2024
Copyright © 2003-2024 Jan Goyvaerts. All rights reserved.