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

加载更多搜索结果...

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

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

配对文本

将整个正则表达式配对重新插入置换文本中,让搜索和置换可以在正则表达式配对前后插入文本,而不用实际置换任何内容。您也可以用包含正则表达式配对的内容来置换正则表达式配对。例如,您可以将正则表达式 https?://\S+ 的所有配对置换成 <a href="$&">$&</a>,将文件中的所有网址转换成链接到这些网址的 HTML锚点。

在 Delphi、.NET、JavaScript 和 VBScript 中,$& 会在置换文本中以整个正则表达式配对取代。在 Perl 中,$& 也是保存整个正则表达式配对的变量。\& 会在 Delphi 和 Ruby 中运作。在 Tcl 中,& 本身就代表整个正则表达式配对,而 $& 是美元符号后接整个正则表达式配对,\& 则是一个字面上的&符号。

在 Boost 和 std::regex 中,您选择的替换格式会改变 & 和 $& 的含义。使用 sed 替换格式时,& 代表整个 regex 匹配,而 $& 是字面上的美元符号,后跟整个 regex 匹配。使用缺省 (ECMAScript) 或「all」替换格式时,& 是字面上的和号,而 $& 代表整个 regex 匹配。

整体 regex 匹配通常被视为一个隐含的 捕获组 编号零。在许多应用程序中,您可以使用 替换文本中的反向引用 语法来参照群组编号零,从而将整个 regex 匹配插入替换文本中。您可以在 Delphi、.NET、Java、XRegExp、PCRE2、PHP 和 XPath 中使用 $0 来运行此操作。\0 可与 Delphi、Ruby、PHP 和 Tcl 搭配使用。

Python 不支持上述任何一种。在 Python 中,您可以使用群组编号零的命名反向引用语法来重新插入整个 regex 匹配:\g<0>。Delphi 不支持此功能,即使它使用此语法支持命名反向引用。

配對文字
  • 简
  • 繁
  • En
關於正規表示式 » 替換字串教學 » 配對文字

置換文字教學
簡介
字元
不可列印字元
配對文字
反向參照
配對內容
大小寫轉換
條件式
本網站其他資訊
簡介
正規表示式快速入門
正規表示式教學
置換字串教學
應用程式和語言
正規表示式範例
正規表示式參考
置換字串參考

配對文字

將整個正規表示式配對重新插入置換文字中,讓搜尋和置換可以在正規表示式配對前後插入文字,而不用實際置換任何內容。您也可以用包含正規表示式配對的內容來置換正規表示式配對。例如,您可以將正規表示式 https?://\S+ 的所有配對置換成 <a href="$&">$&</a>,將檔案中的所有網址轉換成連結到這些網址的 HTML錨點。

在 Delphi、.NET、JavaScript 和 VBScript 中,$& 會在置換文字中以整個正規表示式配對取代。在 Perl 中,$& 也是儲存整個正規表示式配對的變數。\& 會在 Delphi 和 Ruby 中運作。在 Tcl 中,& 本身就代表整個正規表示式配對,而 $& 是美元符號後接整個正規表示式配對,\& 則是一個字面上的&符號。

在 Boost 和 std::regex 中,您選擇的替換格式會改變 & 和 $& 的含義。使用 sed 替換格式時,& 代表整個 regex 匹配,而 $& 是字面上的美元符號,後跟整個 regex 匹配。使用預設 (ECMAScript) 或「all」替換格式時,& 是字面上的和號,而 $& 代表整個 regex 匹配。

整體 regex 匹配通常被視為一個隱含的 擷取群組 編號零。在許多應用程式中,您可以使用 替換文字中的反向參照 語法來參照群組編號零,從而將整個 regex 匹配插入替換文字中。您可以在 Delphi、.NET、Java、XRegExp、PCRE2、PHP 和 XPath 中使用 $0 來執行此操作。\0 可與 Delphi、Ruby、PHP 和 Tcl 搭配使用。

Python 不支援上述任何一種。在 Python 中,您可以使用群組編號零的命名反向參照語法來重新插入整個 regex 匹配:\g<0>。Delphi 不支援此功能,即使它使用此語法支援命名反向參照。

Matched Text
  • 简
  • 繁
  • En
About Regular Expressions » Replacement Strings Tutorial » Matched Text

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

Matched Text

Reinserting the entire regex match into the replacement text allows a search-and-replace to insert text before and after regular expression matches without really replacing anything. It also allows you to replace the regex match with something that contains the regex match. For example, you could replace all matches of the regex https?://\S+ with <a href="$&">$&</a> to turn all URLs in a file into HTML anchors that link to those URLs.

$& is substituted with the whole regex match in the replacement text in Delphi, .NET, JavaScript, and VBScript. It is also the variable that holds the whole regex match in Perl. \& works in Delphi, and Ruby. In Tcl, & all by itself represents the whole regex match, while $& is a literal dollar sign followed by the whole regex match, and \& is a literal ampersand.

In Boost and std::regex your choice of replacement format changes the meaning of & and $&. When using the sed replacement format, & represents the whole regex match and $& is a literal dollar sign followed by the whole regex match. When using the default (ECMAScript) or “all” replacement format, & is a literal ampersand and $& represents the whole regex match.

The overall regex match is usually treated as an implied capturing group number zero. In many applications you can use the syntax for backreferences in the replacement text to reference group number zero and thus insert the whole regex match in the replacement text. You can do this with $0 in Delphi, .NET, Java, XRegExp, PCRE2, PHP, and XPath. \0 works with Delphi, Ruby, PHP, and Tcl.

Python does not support any of the above. In Python you can reinsert the whole regex match by using the syntax for named backreferences with group number zero: \g<0>. Delphi does not support this, even though it supports named backreferences using this syntax.

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