2.19. Insert Literal Text into the Replacement Text
Search and replace any regular expression match literally
with the eight characters
|Replacement text flavor: Java|
|Replacement text flavor: PHP|
|Replacement text flavor: Perl|
|Replacement text flavors: Python, Ruby|
When and how to escape characters in replacement text
This recipe shows you the different escape rules used by the various replacement text flavors. The only two characters you may ever need to escape in the replacement text are the dollar sign and the backslash. The escape characters are also the dollar sign and the backslash.
The percentage sign and asterisk in this example are always
literal characters, though a preceding backslash may be treated as an
escape instead of a literal backslash. «
$1» and/or «
\1» are a backreference to a capturing
group. Recipe 2.21 tells you which
flavors use which syntax for backreferences.
The fact that this problem has five different solutions for seven replacement text flavors demonstrates that there really is no standard for replacement text syntax.
A lone dollar sign is a literal character. Dollar signs need to be escaped only when they are followed by a digit, ampersand, backtick, straight ...