Match Context
Some applications support special tokens in replacement strings that allow you to insert the subject string or the part of the subject string before or after the regex match. This can be useful when the replacement text syntax is used to collect search matches and their context instead of making replacements in the subject string.
In the replacement text, $`
(dollar backtick) is substituted with the part of the subject string to the left of the regex match in Delphi, .NET, JavaScript, VBScript, Boost, and std::regex. It is also the variable that holds the part of the subject string to the left of the regex match in Perl. \`
(backslash backtick) works in Delphi, and Ruby.
In the same applications, you can use $'
(dollar quote) or \'
(backslash quote) to insert the part of the subject string to the right of the regex match.
In the replacement text, $_
is substituted with the entire subject string in Delphi, and .NET. In Perl, $_
is the default variable that the regex is applied to if you use a regular expression without the matching operator =~
. \_
is just an escaped underscore. It has no special meaning in any application.
Boost 1.42 added some alternative syntax of its own invention. $PREMATCH
and ${^PREMATCH}
are synonyms for $`
. $POSTMATCH
and ${^POSTMATCH}
are synonyms for $'
.