Substitutions

Substitutions are allowed only within replacement patterns. For similar functionality within regular expressions, use a backreference (for example, \1). For details on backreferences, see Backreferences and Backreference Constructs.

Character escapes and substitutions are the only special constructs recognized in a replacement pattern. All the syntactic constructs described in the following sections are allowed only in regular expressions; they are not recognized in replacement patterns. For example, the replacement pattern a*${txt}b inserts the string "a*" followed by the substring matched by the txt capturing group, if any, followed by the string "b". The * character is not recognized as a metacharacter within a replacement pattern. Similarly, $ patterns are not recognized within regular expression matching patterns. Within regular expressions, $ designates the end of the string.

The following table shows how to define named and numbered replacement patterns.

Character Description
$number Substitutes the last substring matched by group number number (decimal).
${name} Substitutes the last substring matched by a (?<name> ) group.
$$ Substitutes a single "$" literal.
$& Substitutes a copy of the entire match itself.
$` Substitutes all the text of the input string before the match.
$' Substitutes all the text of the input string after the match.
$+ Substitutes the last group captured.
$_ Substitutes the entire input string.

See Also

Regular Expression Language Elements