
You only need to escape the backslash to suppress its special meaning in combination with other characters. You can use a backslash to escape the backslash. In Tcl, the ampersand & has a special meaning, and must be escaped with a backslash if you want a literal ampersand in your replacement text. A single backslash at the end of the replacement text is ignored. \ ! replaces with a literal exclamation point, and \\ replaces with a single backslash. A backslash always escapes the character that follows. In R, the dollar sign has no special meaning. So $, $$, and \$ all replace with a single dollar sign. You can escape dollar signs with a backslash or with another dollar sign. An unescaped dollar sign is a literal dollar sign if it doesn’t form a replacement string token. In Boost, a backslash always escapes the character that follows. You can’t and needn’t escape the exclamation point or any other character except the backslash and dollar, because they have no special meaning in PHP replacement strings. In \ !, the backslash is a literal character because it doesn’t have a special meaning in combination with the exclamation point. You only need to escape them to suppress their special meaning in combination with other characters. \\ replaces with a literal backslash, while \$ replaces with a literal dollar sign. In PHP’s preg_replace, you can use a backslash to escape the backslash and the dollar. An unescaped backslash at the end of the replacement text, however, is an error in Python but a literal backslash in Ruby. You can’t and needn’t escape the exclamation point or any other character except the backslash, because they have no special meaning in Python and Ruby replacement strings. In Python and Ruby, the dollar sign has no special meaning.

A single backslash at the end of the replacement text is an error.

$ ! is an error because the dollar sign is not escaped and has no special meaning in combination with the exclamation point. You must escape the dollar sign with a backslash or another dollar sign to use it as a literal character. In Java, an unescaped dollar sign that doesn’t form a token is an error. NET, JavaScript, VBScript, and PCRE2 replacement strings. You can’t and needn’t escape the backslash, exclamation point, or any other character except dollar, because they have no special meaning in. In $ \ and $ !, the dollar is a literal character because it doesn’t have a special meaning in combination with the backslash or exclamation point. NET, JavaScript (without XRegExp), and VBScript you only need to escape the dollar sign to suppress its special meaning in combination with other characters. They treat unescaped dollar signs that don’t form valid replacement text tokens as errors. XRegExp and PCRE2 require you to escape all literal dollar signs. NET, JavaScript, VBScript, XRegExp, PCRE2, and std::regex you can escape the dollar sign with another dollar sign. You can’t and needn’t escape the exclamation point or any other character except the backslash and dollar, because they have no special meaning in JGsoft and Delphi replacement strings. In \ ! and $ !, the backslash and dollar are literal characters because they don’t have a special meaning in combination with the exclamation point. \\ replaces with a literal backslash, while \$ and $$ replace with a literal dollar sign. In the JGsoft flavor and Delphi, you can use a backslash to escape the backslash and the dollar, and you can use a dollar to escape the dollar. In other applications, you only need to escape them if they would form a replacement text token with the character that follows. In some applications, you always need to escape them when you want to use them as literal characters.
REGEX SPECIAL CHARACTERS HOW TO
Whether and how to escape them depends on the application you’re using.

In most replacement text flavors, two characters tend to have special meanings: the backslash \ and the dollar sign $.

The replacement replacement simply replaces each regex match with the text replacement.īecause we want to be able to do more than simply replace each regex match with the exact same text, we need to reserve certain characters for special use. The most basic replacement string consists only of literal characters.
