特殊字符特殊字符特殊字符特殊字符
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2024年3月5日
类别
  • 正则表达式
标签
特殊字符
  • 简
  • 繁
  • En
关于正则表达式 » 替换字符串教程 » 特殊字符

替换文本教学
简介
字符
不可打印字符
匹配文本
反向引用
匹配内容
大小写转换
条件
更多本网站信息
简介
正则表达式快速开始
正则表达式教程
替换字符串教程
应用程序和语言
正则表达式范例
正则表达式参考
替换字符串参考

特殊字符

最基本的替换字符串仅包含字面字符。替换 replacement 仅以文本 replacement 替换每个正则表达式匹配项。

由于我们希望能够做的事不只是以完全相同的文本替换每个正则表达式匹配项,因此我们需要保留特定字符以供特殊用途。在大部分的替换文本风格中,两个字符往往具有特殊含义:反斜线 \ 和美元符号 $。是否以及如何转义这些字符取决于您所使用的应用程序。在某些应用程序中,当您想要将这些字符用作字面字符时,您总是需要转义它们。在其他应用程序中,您仅需要在这些字符与后面的字符形成替换文本代码时才转义它们。

在 Delphi 中,您可以使用反斜线转义反斜线和美元符号,并且可以使用美元符号转义美元符号。 \\ 以字面反斜线替换,而 \$ 和 $$ 以字面美元符号替换。您仅需要转义它们以抑制它们与其他字符组合时的特殊含义。在 \! 和 $! 中,反斜线和美元符号是字面字符,因为它们与惊叹号组合时没有特殊含义。您无法转义惊叹号或任何其他字符,也不需要这么做,因为它们在 Delphi 替换字符串中没有特殊含义。

在 .NET、JavaScript、VBScript、XRegExp、PCRE2 和 std::regex 中,你可以使用另一个美元符号来转义美元符号。 $$ 会替换成一个字面美元符号。XRegExp 和 PCRE2 要求你转义所有字面美元符号。它们将未转义且未形成有效替换文本代码的美元符号视为错误。在 .NET、JavaScript(不含 XRegExp)和 VBScript 中,你只需要转义美元符号,就能抑制它与其他字符组合时的特殊意义。在 $\ 和 $! 中,美元符号是一个字面字符,因为它与反斜线或惊叹号组合时没有特殊意义。你无法且不需要转义反斜线、惊叹号或任何其他字符(美元符号除外),因为它们在 .NET、JavaScript、VBScript 和 PCRE2 替换字符串中没有特殊意义。

在 Java 中,未转义且未形成代码的美元符号会产生错误。你必须使用反斜线或另一个美元符号来转义美元符号,才能将它用作字面字符。 $! 会产生错误,因为美元符号未转义,且与惊叹号组合时没有特殊意义。反斜线总是会转义后面的字符。 \! 会替换成一个字面惊叹号,而 \\ 会替换成一个单一反斜线。替换文本结尾的单一反斜线会产生错误。

在 Python 和 Ruby 中,美元符号没有特殊意义。你可以使用反斜线来转义反斜线。你只需要转义反斜线,就能抑制它与其他字符组合时的特殊意义。在 \! 中,反斜线是一个字面字符,因为它与惊叹号组合时没有特殊意义。你无法且不需要转义惊叹号或任何其他字符(反斜线除外),因为它们在 Python 和 Ruby 替换字符串中没有特殊意义。不过,替换文本结尾的未转义反斜线在 Python 中会产生错误,在 Ruby 中则会变成一个字面反斜线。

在 PHP 的 preg_replace 中,你可以使用反斜线来转义反斜线和美元符号。 \\ 会替换成一个字面反斜线,而 \$ 会替换成一个字面美元符号。你只需要转义它们,就能抑制它们与其他字符组合时的特殊意义。在 \! 中,反斜线是一个字面字符,因为它与惊叹号组合时没有特殊意义。你无法且不需要转义惊叹号或任何其他字符(反斜线和美元符号除外),因为它们在 PHP 替换字符串中没有特殊意义。

在 Boost 中,反斜线总是转义后面的字符。\! 以一个字面上的惊叹号取代,而 \\ 以一个单一的反斜线取代。取代文本结尾的单一反斜线会被忽略。未转义的美元符号是一个字面上的美元符号,如果它没有形成一个取代字符串的记号。你可以用反斜线或另一个美元符号来转义美元符号。因此 $、$$ 和 \$ 都以一个单一的美元符号取代。

在 R 中,美元符号没有特殊意义。反斜线总是转义后面的字符。\! 以一个字面上的惊叹号取代,而 \\ 以一个单一的反斜线取代。取代文本结尾的单一反斜线会被忽略。

在 Tcl 中,连字符 & 有特殊意义,如果你想要在取代文本中有一个字面上的连字符,就必须用反斜线转义它。你可以用反斜线来转义反斜线。你只需要转义反斜线,以抑制它与其他字符组合时所具有的特殊意义。在 \! 中,反斜线是一个字面上的字符,因为它与惊叹号组合时没有特殊意义。你不能也不需要转义惊叹号或任何其他字符,除了反斜线和连字符之外,因为它们在 Tcl 取代字符串中没有特殊意义。取代文本结尾的未转义反斜线是一个字面上的反斜线。

在 XPath 中,未转义的反斜线是一个错误。未转义且没有形成记号的美元符号也是一个错误。你必须用反斜线转义反斜线和美元符号,才能将它们用作字面上的字符。反斜线没有其他特殊意义,除了转义另一个反斜线或美元符号。

Perl 是个特例。Perl 没有真正的替换文本语法。因此,它也没有替换文本的转义规则。在 Perl 原代码中,替换字符串只是双引号字符串。反向引用在替换文本中看起来像,实际上是内插变量。您可以在正规表达式配对后,在任何其他双引号字符串中内插它们,即使不运行搜索和替换。

特殊字符和编程语言

前一节的规则说明了这些编程语言中的搜索和替换函数如何剖析替换文本。如果您的应用程序从用户输入接收替换文本,则您的应用程序的用户必须遵循这些转义规则,而且只能遵循这些规则。您可能会惊讶地发现单引号和双引号等字符并非特殊字符。这是正确的。当使用正规表达式或 grep 工具或文本编辑器的搜索和替换函数)时,您不应该像在编程语言中那样转义或重复引号字符。

如果您在原代码中将替换文本指定为字符串常数,则必须记住编程语言在字符串常数中对哪些字符进行特殊处理。这是因为这些字符是由编译器处理的,在替换文本函数看到字符串之前。例如,Java 要用单一美元符号替换所有正规表达式配对,您需要使用替换文本 \$,您需要在原代码中输入 "\\$"。Java 编译器将原代码中的转义反斜线转换为传递给 replaceAll() 函数的字符串中的单一反斜线。然后,该函数将单一反斜线和美元符号视为转义的美元符号。

请参阅本网站的 工具和语言 部分,以了解有关如何在各种编程语言中使用替换字符串的更多信息。

特殊字元
  • 简
  • 繁
  • En
關於正規表示式 » 替換字串教學 » 特殊字元

替換文字教學
簡介
字元
不可列印字元
匹配文字
反向參照
匹配內容
大小寫轉換
條件
更多本網站資訊
簡介
正規表示式快速開始
正規表示式教學
替換字串教學
應用程式和語言
正規表示式範例
正規表示式參考
替換字串參考

特殊字元

最基本的替換字串僅包含字面字元。替換 replacement 僅以文字 replacement 替換每個正規表示式匹配項。

由於我們希望能夠做的事不只是以完全相同的文字替換每個正規表示式匹配項,因此我們需要保留特定字元以供特殊用途。在大部分的替換文字風格中,兩個字元往往具有特殊含義:反斜線 \ 和美元符號 $。是否以及如何跳脫這些字元取決於您所使用的應用程式。在某些應用程式中,當您想要將這些字元用作字面字元時,您總是需要跳脫它們。在其他應用程式中,您僅需要在這些字元與後面的字元形成替換文字代碼時才跳脫它們。

在 Delphi 中,您可以使用反斜線跳脫反斜線和美元符號,並且可以使用美元符號跳脫美元符號。 \\ 以字面反斜線替換,而 \$ 和 $$ 以字面美元符號替換。您僅需要跳脫它們以抑制它們與其他字元組合時的特殊含義。在 \! 和 $! 中,反斜線和美元符號是字面字元,因為它們與驚嘆號組合時沒有特殊含義。您無法跳脫驚嘆號或任何其他字元,也不需要這麼做,因為它們在 Delphi 替換字串中沒有特殊含義。

在 .NET、JavaScript、VBScript、XRegExp、PCRE2 和 std::regex 中,你可以使用另一個美元符號來跳脫美元符號。 $$ 會替換成一個字面美元符號。XRegExp 和 PCRE2 要求你跳脫所有字面美元符號。它們將未跳脫且未形成有效替換文字代碼的美元符號視為錯誤。在 .NET、JavaScript(不含 XRegExp)和 VBScript 中,你只需要跳脫美元符號,就能抑制它與其他字元組合時的特殊意義。在 $\ 和 $! 中,美元符號是一個字面字元,因為它與反斜線或驚嘆號組合時沒有特殊意義。你無法且不需要跳脫反斜線、驚嘆號或任何其他字元(美元符號除外),因為它們在 .NET、JavaScript、VBScript 和 PCRE2 替換字串中沒有特殊意義。

在 Java 中,未跳脫且未形成代碼的美元符號會產生錯誤。你必須使用反斜線或另一個美元符號來跳脫美元符號,才能將它用作字面字元。 $! 會產生錯誤,因為美元符號未跳脫,且與驚嘆號組合時沒有特殊意義。反斜線總是會跳脫後面的字元。 \! 會替換成一個字面驚嘆號,而 \\ 會替換成一個單一反斜線。替換文字結尾的單一反斜線會產生錯誤。

在 Python 和 Ruby 中,美元符號沒有特殊意義。你可以使用反斜線來跳脫反斜線。你只需要跳脫反斜線,就能抑制它與其他字元組合時的特殊意義。在 \! 中,反斜線是一個字面字元,因為它與驚嘆號組合時沒有特殊意義。你無法且不需要跳脫驚嘆號或任何其他字元(反斜線除外),因為它們在 Python 和 Ruby 替換字串中沒有特殊意義。不過,替換文字結尾的未跳脫反斜線在 Python 中會產生錯誤,在 Ruby 中則會變成一個字面反斜線。

在 PHP 的 preg_replace 中,你可以使用反斜線來跳脫反斜線和美元符號。 \\ 會替換成一個字面反斜線,而 \$ 會替換成一個字面美元符號。你只需要跳脫它們,就能抑制它們與其他字元組合時的特殊意義。在 \! 中,反斜線是一個字面字元,因為它與驚嘆號組合時沒有特殊意義。你無法且不需要跳脫驚嘆號或任何其他字元(反斜線和美元符號除外),因為它們在 PHP 替換字串中沒有特殊意義。

在 Boost 中,反斜線總是跳脫後面的字元。\! 以一個字面上的驚嘆號取代,而 \\ 以一個單一的反斜線取代。取代文字結尾的單一反斜線會被忽略。未跳脫的美元符號是一個字面上的美元符號,如果它沒有形成一個取代字串的記號。你可以用反斜線或另一個美元符號來跳脫美元符號。因此 $、$$ 和 \$ 都以一個單一的美元符號取代。

在 R 中,美元符號沒有特殊意義。反斜線總是跳脫後面的字元。\! 以一個字面上的驚嘆號取代,而 \\ 以一個單一的反斜線取代。取代文字結尾的單一反斜線會被忽略。

在 Tcl 中,連字元 & 有特殊意義,如果你想要在取代文字中有一個字面上的連字元,就必須用反斜線跳脫它。你可以用反斜線來跳脫反斜線。你只需要跳脫反斜線,以抑制它與其他字元組合時所具有的特殊意義。在 \! 中,反斜線是一個字面上的字元,因為它與驚嘆號組合時沒有特殊意義。你不能也不需要跳脫驚嘆號或任何其他字元,除了反斜線和連字元之外,因為它們在 Tcl 取代字串中沒有特殊意義。取代文字結尾的未跳脫反斜線是一個字面上的反斜線。

在 XPath 中,未跳脫的反斜線是一個錯誤。未跳脫且沒有形成記號的美元符號也是一個錯誤。你必須用反斜線跳脫反斜線和美元符號,才能將它們用作字面上的字元。反斜線沒有其他特殊意義,除了跳脫另一個反斜線或美元符號。

Perl 是個特例。Perl 沒有真正的替換文字語法。因此,它也沒有替換文字的跳脫規則。在 Perl 原始碼中,替換字串只是雙引號字串。反向參照在替換文字中看起來像,實際上是內插變數。您可以在正規運算式配對後,在任何其他雙引號字串中內插它們,即使不執行搜尋和替換。

特殊字元和程式語言

前一節的規則說明了這些程式語言中的搜尋和替換函式如何剖析替換文字。如果您的應用程式從使用者輸入接收替換文字,則您的應用程式的使用者必須遵循這些跳脫規則,而且只能遵循這些規則。您可能會驚訝地發現單引號和雙引號等字元並非特殊字元。這是正確的。當使用正規運算式或 grep 工具或文字編輯器的搜尋和替換函式)時,您不應該像在程式語言中那樣跳脫或重複引號字元。

如果您在原始碼中將替換文字指定為字串常數,則必須記住程式語言在字串常數中對哪些字元進行特殊處理。這是因為這些字元是由編譯器處理的,在替換文字函式看到字串之前。例如,Java 要用單一美元符號替換所有正規運算式配對,您需要使用替換文字 \$,您需要在原始碼中輸入 "\\$"。Java 編譯器將原始碼中的跳脫反斜線轉換為傳遞給 replaceAll() 函式的字串中的單一反斜線。然後,該函式將單一反斜線和美元符號視為跳脫的美元符號。

請參閱本網站的 工具和語言 部分,以瞭解有關如何在各種程式語言中使用替換字串的更多資訊。

Special Characters
  • 简
  • 繁
  • En
About Regular Expressions » Replacement Strings Tutorial » Special Characters

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

Special Characters

The most basic replacement string consists only of literal characters. The replacement replacement simply replaces each regex match with the text replacement.

Because 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. In most replacement text flavors, two characters tend to have special meanings: the backslash \ and the dollar sign $. Whether and how to escape them depends on the application you’re using. In some applications, you always need to escape them when you want to use them as literal characters. In other applications, you only need to escape them if they would form a replacement text token with the character that follows.

In Delphi, you can use a backslash to escape the backslash and the dollar, and you can use a dollar to escape the dollar. \\ replaces with a literal backslash, while \$ and $$ replace with a literal dollar sign. You only need to escape them to suppress their special meaning in combination with other characters. In \! and $!, the backslash and dollar are literal characters because they don’t have a special meaning in combination with the exclamation point. 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 Delphi replacement strings.

In .NET, JavaScript, VBScript, XRegExp, PCRE2, and std::regex you can escape the dollar sign with another dollar sign. $$ replaces with a literal dollar sign. XRegExp and PCRE2 require you to escape all literal dollar signs. They treat unescaped dollar signs that don’t form valid replacement text tokens as errors. In .NET, JavaScript (without XRegExp), and VBScript you only need to escape the dollar sign to suppress its special meaning in combination with other characters. In $\ and $!, the dollar is a literal character because it doesn’t have a special meaning in combination with the backslash or exclamation point. You can’t and needn’t escape the backslash, exclamation point, or any other character except dollar, because they have no special meaning in .NET, JavaScript, VBScript, and PCRE2 replacement strings.

In Java, an unescaped dollar sign that doesn’t form a token is an error. You must escape the dollar sign with a backslash or another dollar sign to use it as a literal character. $! is an error because the dollar sign is not escaped and has no special meaning in combination with the exclamation point. A backslash always escapes the character that follows. \! replaces with a literal exclamation point, and \\ replaces with a single backslash. A single backslash at the end of the replacement text is an error.

In Python and Ruby, the dollar sign has no special meaning. You can use a backslash to escape the backslash. You only need to escape the backslash to suppress its special meaning in combination with other characters. In \!, the backslash is a literal character because it doesn’t have a special meaning in combination with the exclamation point. 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. An unescaped backslash at the end of the replacement text, however, is an error in Python but a literal backslash in Ruby.

In PHP’s preg_replace, you can use a backslash to escape the backslash and the dollar. \\ replaces with a literal backslash, while \$ replaces with a literal dollar sign. You only need to escape them to suppress their special meaning in combination with other characters. In \!, the backslash is a literal character because it doesn’t have a special meaning in combination with the exclamation point. 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 Boost, a backslash always escapes the character that follows. \! replaces with a literal exclamation point, and \\ replaces with a single backslash. A single backslash at the end of the replacement text is ignored. An unescaped dollar sign is a literal dollar sign if it doesn’t form a replacement string token. You can escape dollar signs with a backslash or with another dollar sign. So $, $$, and \$ all replace with a single dollar sign.

In R, the dollar sign has no special meaning. A backslash always escapes the character that follows. \! replaces with a literal exclamation point, and \\ replaces with a single backslash. A single backslash at the end of the replacement text is ignored.

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. You can use a backslash to escape the backslash. You only need to escape the backslash to suppress its special meaning in combination with other characters. In \!, the backslash is a literal character because it doesn’t have a special meaning in combination with the exclamation point. You can’t and needn’t escape the exclamation point or any other character except the backslash and ampersand, because they have no special meaning in Tcl replacement strings. An unescaped backslash at the end of the replacement text is a literal backslash.

In XPath, an unescaped backslash is an error. An unescaped dollar sign that doesn’t form a token is also an error. You must escape backslashes and dollars with a backslash to use them as literal characters. The backslash has no special meaning other than to escape another backslash or a dollar sign.

Perl is a special case. Perl doesn’t really have a replacement text syntax. So it doesn’t have escape rules for replacement texts either. In Perl source code, replacement strings are simply double-quoted strings. What looks like backreferences in replacement text are really interpolated variables. You could interpolate them in any other double-quoted string after a regex match, even when not doing a search-and-replace.

Special Characters and Programming Languages

The rules in the previous section explain how the search-and-replace functions in these programming languages parse the replacement text. If your application receives the replacement text from user input, then the user of your application would have to follow these escape rules, and only these rules. You may be surprised that characters like the single quote and double quote are not special characters. That is correct. When using a grep tool or the search-and-replace function of a text editor, you should not escape or repeat the quote characters like you do in a programming language.

If you specify the replacement text as a string constant in your source code, then you have to keep in mind which characters are given special treatment inside string constants by your programming language. That is because those characters are processed by the compiler, before the replacement text function sees the string. So Java, for example, to replace all regex matches with a single dollar sign, you need to use the replacement text \$, which you need to enter in your source code as "\\$". The Java compiler turns the escaped backslash in the source code into a single backslash in the string that is passed on to the replaceAll() function. That function then sees the single backslash and the dollar sign as an escaped dollar sign.

See the tools and languages section of this website for more information on how to use replacement strings in various programming languages.

©2015-2025 艾丽卡 support@alaica.com