Syntax
|
Description
|
Example
|
Characters
|
A backslash that does not form a token
|
A backslash that is not part of a replacement string token is a literal backslash.
|
Replacing with \! yields \!
|
Trailing backslash
|
A backslash at the end of the replacement string is a literal backslash.
|
Replacing with \ yields \
|
\\
|
A backslash escapes itself.
|
Replacing with \\ yields \
|
A dollar that does not form a token
|
A dollar sign that does not form a replacement string token is a literal dollar sign.
|
Replacing with $! yields $!
|
Trailing dollar
|
A dollar sign at the end of the replacement string is a literal dollar sign.
|
Replacing with $ yields $
|
$$
|
A dollar sign escapes itself.
|
Replacing with $$ yields $
|
\$
|
A backslash escapes a dollar sign.
|
Replacing with \$ yields $
|
\n
|
Line feed
|
|
\r
|
Carriage Return
|
|
\R
|
CRLF
|
|
Matched Text and Backreferences
|
\&
|
Insert the whole regex match.
|
Replacing \d+ with [\&] in 1a2b yields [1]a[2]b
|
$&
|
Insert the whole regex match.
|
Replacing \d+ with [$&] in 1a2b yields [1]a[2]b
|
\0
|
Insert the whole regex match.
|
Replacing \d+ with [\0] in 1a2b yields [1]a[2]b
|
$0
|
Insert the whole regex match.
|
Replacing \d+ with [$0] in 1a2b yields [1]a[2]b
|
\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
|
\10 through \99
|
Insert the text matched by capturing groups 10 through 99.
|
|
\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
|
$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
|
$10 through $99
|
Insert the text matched by capturing groups 10 through 99.
|
|
$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
|
${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
|
${name}
|
Insert the text matched by the named capturing group “name”.
|
Replacing (?'one'a)(?'two'b) with ${two}${one} in ab yields ba
|
\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
|
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.
|
|
Any supported backreference syntax
|
A backreference to a non-participating capturing group is replaced with the empty string.
|
|
\+
|
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
|
$+
|
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
|
Context and Case Conversion
|
\‘
|
Insert the part of the subject string to the left of the regex match
|
Replacing b with \‘ in abc yields aac
|
$‘
|
Insert the part of the subject string to the left of the regex match
|
Replacing b with $‘ in abc yields aac
|
\'
|
Insert the part of the subject string to the right of the regex match
|
Replacing b with \' in abc yields acc
|
$'
|
Insert the part of the subject string to the right of the regex match
|
Replacing b with $' in abc yields acc
|
$_
|
Insert the whole subject string
|
Replacing b with $_ in abc yields aabcc
|
\U0 and \U1 through \U99
|
Insert the whole regex match or the 1st through 99th backreference with all letters in the matched text converted to uppercase.
|
Replacing .+ with \U0 in HeLlO WoRlD yields HELLO WORLD
|
\L0 and \L1 through \L99
|
Insert the whole regex match or the 1st through 99th backreference with all letters in the matched text converted to lowercase.
|
Replacing .+ with \L0 in HeLlO WoRlD yields hello world
|
\F0 and \F1 through \F99
|
Insert the whole regex match or the 1st through 99th backreference with the first letter in the matched text converted to uppercase and the remaining letters converted to lowercase.
|
Replacing .+ with \F0 in HeLlO WoRlD yields Hello world
|
\I0 and \I1 through \I99
|
Insert the whole regex match or the 1st through 99th backreference with the first letter of each word in the matched text converted to uppercase and the remaining letters converted to lowercase.
|
Replacing .+ with \I0 in HeLlO WoRlD yields Hello World
|