Quick Start
Tutorial
Tools & Languages
Examples
Reference
Book Reviews
Replacement Text Tutorial
Introduction
Characters
Non-Printable Characters
Matched Text
Backreferences
Match Context
Case Conversion
Conditionals
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

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 the JGsoft applications, 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 the JGsoft applications, 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 the JGsoft applications, 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 $'.