Quick Start
Tutorial
Search & Replace
Tools & Languages
Examples
Reference
Replacement Reference
Introduction
Table of Contents
Characters
Matched Text & Backreferences
Case Conversion
Context
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 Python JavaScript VBScript XRegExp .NET Java ICU RE2 Perl PCRE2 PHP Delphi R Ruby std::regex Boost Tcl Oracle XPath
Ampersand \& Insert a literal ampersand. Replacing with \& yields & nonononononoYESYESerrorYESextendednonoYESnosedYESYESnoerror
Whole match \& Insert the whole regex match. Replacing \d+ with [\&] in 1a2b yields [1]a[2]b YESnononononononoerrornononoYESnoYESnonononoerror
Whole match $& Insert the whole regex match. Replacing \d+ with [$&] in 1a2b yields [1]a[2]b YESnoYESYESYESYESerrorerrornoYES10.45noYESnonodefaultall
default
nonoerror
Whole match & Insert the whole regex match. Replacing \d+ with [&] in 1a2b yields [1]a[2]b nononononononononononononononosedsedYESnono
Whole match \0 Insert the whole regex match. Replacing \d+ with [\0] in 1a2b yields [1]a[2]b YESnononononononoYESnonoYESYESnoYESsedsedYESnoerror
Whole match $0 Insert the whole regex match. Replacing \d+ with [$0] in 1a2b yields [1]a[2]b YESnononoYESYESYESYESnoerrorYESYESYESnonodefault
VC’10–VC’13
all
default
nonoYES
Whole match ${0} Insert the whole regex match. Replacing \d+ with [${0}] in 1a2b yields [1]a[2]b YESnononoYESYESerrorerrornoerrorYESYESYESnononoall
default
nonoerror
Whole match \g<0> Insert the whole regex match. Replacing \d+ with [\g<0>] in 1a2b yields [1]a[2]b YESYESnonononononoerrornoextended
10.45
nonononononononoerror
Whole match $MATCH and ${^MATCH} Insert the whole regex match. Replacing \d+ with [$MATCH] in 1a2b yields [1]a[2]b nonononoerrornoerrorerrornoerrorerrornononononoall
default
1.42
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 YESYESnonononononoYESYESextended
10.45
YESYESYESYESsedYESYESYESerror
Backreference \10 through \99 Insert the text matched by capturing groups 10 through 99. YESYESnonononononononoextended
10.45
YESYESnonononononoerror
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 YESnon/an/an/an/an/an/an/an/anonoYESn/an/an/an/an/an/an/a
Backreference \100 through \999 Insert the text matched by capturing groups 100 through 999. nonononononononononoextended
10.45
nonononononononoerror
Backreference and literal \100 through \999 When there are fewer capturing groups than the 3-digit number, treat this as a 2-digit backreference followed by a literal number instead of as an invalid backreference. Replacing (a)(b)(c) with \345\234\123 in abc yields c45b34a23 n/an/an/an/an/an/an/an/an/an/anon/an/an/an/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 YESnoYESYESYESYESYESYESnoYESYESYESYESnonodefaultall
default
nonoYES
Backreference $10 through $99 Insert the text matched by capturing groups 10 through 99. YESnoYESYESYESYESYESYESnoYESYESYESYESnonodefaultall
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 $345$234$123 in abc yields c45b34a23 YESn/aYESYESnoECMAYESYESn/anononoYESn/an/anonon/an/aYES
Backreference $100 through $999 Insert the text matched by capturing groups 100 through 999. nononononoYESYESYESnoYESYESnononononoall
default
1.42
nonoYES
Backreference and literal $100 through $999 When there are fewer capturing groups than the 3-digit number, treat this as a 2-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 n/an/an/an/an/aECMAYESYESn/anonon/an/an/an/an/anon/an/aYES
Backreference ${1} through ${999} Insert the text matched by capturing groups 1 through 999. Replacing (a)(b)(c) with ${3}${3}${1} in abc yields cca YESnononoYESYESerrorerrornoYESYESYESYESnononoall
default
nonoerror
Backreference \g<1> through \g<999> Insert the text matched by capturing groups 1 through 999. Replacing (a)(b)(c) with \g<3>\g<3>\g<1> in abc yields cca YESYESnonononononoerrornoextended
10.45
nonononononononoerror
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 YESnononoYESYES7
4 error
YESnoerrorYESnoYESnonononononoerror
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 nonononoerrornoerrorerrornoYESerrornononononoall
default
1.42
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 nonononoerrornoerrorerrornoerrorYESnonononononononoerror
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 YESYESnonononononoerrornoextended
10.45
noYESnonononononoerror
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 onlyerrornonoerrornoerrorerror2017/05–2020/05YESerrorYESYESYESYESdefault VC’10
sed VC’15
YESYESnoYES
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 errorerrorYESYESerrorYESerrorerrornonoerrornonononosed
VC’10–VC’13 error
nonoYESno
Invalid backreference Any supported backreference syntax No replacements are made at all if the replacement string contains a backreference that indicates a number greater than the highest-numbered group or a name of a group that does not exist. V2 errorerrornonoerrornoerrorerror2020/06noerrornonononosed
VC’10–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. YES3.5
2.4 error
YESYESYESYESYESYESYESYESerrorYESYESYESYESYESYESYESYESYES
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 YESnononononononoerrornononoYESnoYESnonononoerror
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 nonononononononoerrornonononononononononoerror
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 YESnononoerrornoerrorerrorno5.18errornoYESnonononononoerror
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 nononoYESerrorYESerrorerrorno5.14–5.16errornononononoall
default
1.42
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 nonononoerrornoerrorerrornoYESerrornononononoall
default
1.42
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 nonononoerrornoerrorerrornoerrorerrornononononoall
default
1.42
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 nonononoerrornoerrorerrornoerrorerrornononononoall
default
1.42
nonoerror
FeatureSyntaxDescriptionExampleJGsoft Python JavaScript VBScript XRegExp .NET Java ICU RE2 Perl PCRE2 PHP Delphi R Ruby std::regex Boost Tcl Oracle XPath