Quick Start
Tutorial
Tools & Languages
Examples
Reference
Book Reviews
Replacement Reference
Introduction
Table of Contents
Characters
Matched Text & Backreferences
Context & Case Conversion
Conditionals
Regular Expressions Reference
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
RegexBuddy—Better than a regular expression reference!

Replacement Strings Reference: Matched Text and Backreferences

FeatureSyntaxDescriptionExampleJGsoft .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 & nonoYESYESextendednonoYESnononononosedYESYESnoerror
Whole match \& Insert the whole regex match. Replacing \d+ with [\&] in 1a2b yields [1]a[2]b YESnononononoYESnononononoYESnonononoerror
Whole match $& Insert the whole regex match. Replacing \d+ with [$&] in 1a2b yields [1]a[2]b YESYESerrorYESerrornoYESnoYESYESYESnonodefaultall
default
nonoerror
Whole match & Insert the whole regex match. Replacing \d+ with [&] in 1a2b yields [1]a[2]b nononononononononononononosedsedYESnono
Whole match \0 Insert the whole regex match. Replacing \d+ with [\0] in 1a2b yields [1]a[2]b YESnonononoYESYESnononononoYESsedsedYESnoerror
Whole match $0 Insert the whole regex match. Replacing \d+ with [$0] in 1a2b yields [1]a[2]b YESYESYESerrorYESYESYESnononoYESnonodefault
VC’08–VC’13
all
default
nonoYES
Whole match \g<0> Insert the whole regex match. Replacing \d+ with [\g<0>] in 1a2b yields [1]a[2]b YESnonononononononononoYESnononononoerror
Whole match $MATCH and ${^MATCH} Insert the whole regex match. Replacing \d+ with [$MATCH] in 1a2b yields [1]a[2]b nonoerrorerrorerrornononononoerrornononoall
default
1.42–1.85
nonoerror
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 YESnonoYESnoYESYESYESnononoYESYESsedYESYESYESerror
Backreference \10 through \99 Insert the text matched by capturing groups 10 through 99. YESnonononoYESYESnonononoYESnononononono
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 YESn/an/an/an/anoYESn/an/an/an/anon/an/an/an/an/an/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 YESYESYESYESYESYESYESnoYESYESYESnonodefaultall
default
nonoYES
Backreference $10 through $99 Insert the text matched by capturing groups 10 through 99. YESYESYESYESYESYESYESnoYESYESYESnonodefaultall
default
nonoYES
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 YESECMAYESnononoYESn/aYESYESnon/an/anonon/an/aYES
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 YESYESerrorYESYESYESYESnononoYESnononoall
default
nonoerror
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 YESnonononononononononoYESnononononoerror
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 YESYES7errorYESnoYESnononoYESnonononononoerror
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 nonoerror5.10errornononononoerrornononoall
default
1.42–1.85
nonoerror
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 nonoerrorerrorYESnononononoerrornonononononoerror
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 YESnononononoYESnonononoYESnononononoerror
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 onlynoerrorYESerrorYESYESYESnonoerrorerrorYESdefault VC’08–VC’22
sed VC’15–VC’22
YESYESYESYES
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 errorYESerrornoerrornononoYESYESerrorerrornosed
VC’08–VC’13 error
nononono
Backreference to non-participating group Any supported backreference syntax A backreference to a non-participating capturing group is replaced with the empty string. YESYESYESYESerrorYESYESYESYESYESYES3.5YESYESYESYESYESYES
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 YESnononononoYESnononononoYESnonononoerror
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 nononononononononononononononononoerror
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 YESnoerror5.18errornoYESnononoerrornonononononoerror
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 noYESerror5.8–5.16errornonononoYESerrornononoall
default
1.42–1.85
nonoerror
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 nonoerrorYESerrornononononoerrornononoall
default
1.42–1.85
nonoerror
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 nonoerrorerrorerrornononononoerrornononoall
default
1.42–1.85
nonoerror
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 nonoerrorerrorerrornononononoerrornononoall
default
1.42–1.85
nonoerror
FeatureSyntaxDescriptionExampleJGsoft .NET Java Perl PCRE2 PHP Delphi R JavaScript VBScript XRegExp Python Ruby std::regex Boost Tcl ARE Oracle XPath