编号反向引用编号反向引用编号反向引用编号反向引用
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

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

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

编号反向引用

如果您的正则表达式有 命名 或 编号捕获组,您可以在替换文本中重新插入由任何一个捕获组配对的文本。您的替换文本可以参照任意数量的群组,甚至可以多次参照同一个群组。这使得可以以许多不同的方式重新排列由正则表达式配对的文本。举一个简单的范例,正则表达式 \*(\w+)\* 会配对星号之间的单一字词,将字词保存在第一个 (也是唯一一个) 捕获组中。替换文本 <b>\1</b> 会用捕获组保存的文本,以粗体标签替换每个正则表达式配对。实际上,这个搜索和替换会用粗体标签替换星号,让星号之间的字词保持原样。使用反向引用的这个技巧很重要。将 *word* 整体替换为 <b>word</b> 比想办法正确地个别替换星号容易且有效率得多。

替换文本中反向引用的 \1 语法取自 正则表达式中反向引用的语法。 \1 到 \9 受 Delphi、Perl(尽管已弃用)、Python、Ruby、PHP、R、Boost 和 Tcl 支持。双位数反向引用 \10 到 \99 受 Delphi、Python 和 Boost 支持。如果正则表达式中没有足够的捕获组,让双位数反向引用有效,那么所有这些版本都会将 \10 到 \99 视为单一位数反向引用,后接一个字面数字。支持单一位数反向引用但非双位数反向引用的版本也会这样做。

$1 到 $99(用於单一位数和双位数反向引用)受 Delphi、.NET、Java、JavaScript、VBScript、PCRE2、PHP、Boost、std::regex 和 XPath 支持。这些也是在 Perl 中保存由捕获组配对到的文本的变量。如果正则表达式中没有足够的捕获组,让双位数反向引用有效,那么 $10 到 $99 会被这些版本(.NET、Perl、PCRE2 和 std::regex 除外)视为单一位数反向引用,后接一个字面数字。

在数字周围加上大括号 ${1} 会将数字与任何后接的字面数字隔离。这在 Delphi、.NET、Perl、PCRE2、PHP、Boost 和 XRegExp 中有效。

命名反向引用

如果你的正则表达式有 命名捕获组,那么你应该在取代文本中使用它们的命名反向引用。正则表达式 (?'name'group) 有个名为「name」的群组。你可以在 Delphi、.NET、PCRE2、Java 7 和 XRegExp 中使用 ${name} 来参照这个群组。PCRE2 也支持没有大括弧的 $name。在 Perl 5.10 和更新版本中,你可以内插变量 $+{name}。Boost 也在取代字符串中使用 $+{name}。${name} 在任何版本的 Perl 中都不会运作。$name 是 PCRE2 独有的。

在 Python 中,如果你有正则表达式 (?P<name>group),那么你可以使用 \g<name> 在取代文本中使用它的比对。这个语法也在 Delphi 中运作。Python(但 Delphi 没有)也支持使用这个语法进行编号反向引用。在 Python 中,这是唯一可以让编号反向引用紧接在字面数字之后的方法。

PHP 和 R 在正则表达式中支持命名捕获组和命名反向引用。但是它们不支持在取代文本中使用命名反向引用。你必须在取代文本中使用编号反向引用来重新插入由命名组比对的文本。要找出这些数字,请从左到右计算正则表达式中所有捕获组(命名和未命名)的左括弧。

对不存在捕获组的反向引用

无效反向引用是指对大于正则表达式中捕获组数量的数字,或对正则表达式中不存在的名称的参照。这种反向引用可以用三种不同的方式处理。Delphi、Perl、Ruby、PHP、R、Boost、std::regex、XPath 和 Tcl 会用空字符串取代无效反向引用。Java、XRegExp、PCRE2 和 Python 会将它们视为语法错误。JavaScript(没有 XRegExp)和 .NET 将它们视为字面文本。

对非参与捕获组的反向引用

非参与捕获组是完全未参与比对尝试的群组。这与比对空字符串的群组不同。a(b?)c 中的群组总是参与比对。其内容是选用的,但群组本身不是选用的。a(b)?c 中的群组是选用的。当正则表达式比对 abc 时,它会参与,但当正则表达式比对 ac 时,它不会参与。

在大部分应用程序中,替换字符串中对比对空字符串的群组或未参与的群组的回朔参考之间没有差异。两者都会替换为空字符串。两个例外是 Python 和 PCRE2。它们允许替换字符串中对选用捕获组的回朔参考。但如果捕获组碰巧未参与其中一个正则表达式比对,搜索和取代会在 PCRE2 中传回错误码。在 Python 3.4 及之前版本中,相同的情况会引发例外。Python 3.5 不再引发例外。

对编号最高的群组的回朔参考

在 Delphi 中,$+ 会插入实际参与比对的编号最高的群组比对到的文本。在 Perl 5.18 中,变量 $+ 包含相同的文本。当 (a)(b)|(c)(d) 比对 ab 时,$+ 会替换为 b。当相同的正则表达式比对 cd 时,$+ 会插入 d。\+ 在 Delphi 和 Ruby 中运行相同的动作。

在 .NET、VBScript 和 Boost 中,$+ 会插入编号最高的群组比对到的文本,不论它是否参与比对。如果它没有参与,则不会插入任何东西。在 Perl 5.16 及之前版本中,变量 $+ 包含相同的文本。当 (a)(b)|(c)(d) 比对 ab 时,$+ 会替换为空字符串。当相同的正则表达式比对 cd 时,$+ 会插入 d。

Boost 1.42 添加了它自己发明的其他语法,用于编号最高的群组的任何意义。$^N、$LAST_SUBMATCH_RESULT 和 ${^LAST_SUBMATCH_RESULT} 都会插入实际参与比对的编号最高的群组比对到的文本。$LAST_PAREN_MATCH 和 ${^LAST_PAREN_MATCH} 都会插入编号最高的群组比对到的文本,不论它是否参与比对。

編號反向參照
  • 简
  • 繁
  • En
關於正規表示式 » 替換字串教學 » 編號反向參照

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

編號反向參照

如果您的正規表示式有 命名 或 編號擷取群組,您可以在替換文字中重新插入由任何一個擷取群組配對的文字。您的替換文字可以參照任意數量的群組,甚至可以多次參照同一個群組。這使得可以以許多不同的方式重新排列由正規表示式配對的文字。舉一個簡單的範例,正規表示式 \*(\w+)\* 會配對星號之間的單一字詞,將字詞儲存在第一個 (也是唯一一個) 擷取群組中。替換文字 <b>\1</b> 會用擷取群組儲存的文字,以粗體標籤替換每個正規表示式配對。實際上,這個搜尋和替換會用粗體標籤替換星號,讓星號之間的字詞保持原樣。使用反向參照的這個技巧很重要。將 *word* 整體替換為 <b>word</b> 比想辦法正確地個別替換星號容易且有效率得多。

替換文字中反向引用的 \1 語法取自 正規表示式中反向引用的語法。 \1 到 \9 受 Delphi、Perl(儘管已棄用)、Python、Ruby、PHP、R、Boost 和 Tcl 支援。雙位數反向引用 \10 到 \99 受 Delphi、Python 和 Boost 支援。如果正規表示式中沒有足夠的擷取群組,讓雙位數反向引用有效,那麼所有這些版本都會將 \10 到 \99 視為單一位數反向引用,後接一個字面數字。支援單一位數反向引用但非雙位數反向引用的版本也會這樣做。

$1 到 $99(用於單一位數和雙位數反向引用)受 Delphi、.NET、Java、JavaScript、VBScript、PCRE2、PHP、Boost、std::regex 和 XPath 支援。這些也是在 Perl 中儲存由擷取群組配對到的文字的變數。如果正規表示式中沒有足夠的擷取群組,讓雙位數反向引用有效,那麼 $10 到 $99 會被這些版本(.NET、Perl、PCRE2 和 std::regex 除外)視為單一位數反向引用,後接一個字面數字。

在數字周圍加上大括號 ${1} 會將數字與任何後接的字面數字隔離。這在 Delphi、.NET、Perl、PCRE2、PHP、Boost 和 XRegExp 中有效。

命名反向引用

如果你的正規表示式有 命名擷取群組,那麼你應該在取代文字中使用它們的命名反向參照。正規表示式 (?'name'group) 有個名為「name」的群組。你可以在 Delphi、.NET、PCRE2、Java 7 和 XRegExp 中使用 ${name} 來參照這個群組。PCRE2 也支援沒有大括弧的 $name。在 Perl 5.10 和更新版本中,你可以內插變數 $+{name}。Boost 也在取代字串中使用 $+{name}。${name} 在任何版本的 Perl 中都不會運作。$name 是 PCRE2 獨有的。

在 Python 中,如果你有正規表示式 (?P<name>group),那麼你可以使用 \g<name> 在取代文字中使用它的比對。這個語法也在 Delphi 中運作。Python(但 Delphi 沒有)也支援使用這個語法進行編號反向參照。在 Python 中,這是唯一可以讓編號反向參照緊接在字面數字之後的方法。

PHP 和 R 在正規表示式中支援命名擷取群組和命名反向參照。但是它們不支援在取代文字中使用命名反向參照。你必須在取代文字中使用編號反向參照來重新插入由命名群組比對的文字。要找出這些數字,請從左到右計算正規表示式中所有擷取群組(命名和未命名)的左括弧。

對不存在擷取群組的反向參照

無效反向參照是指對大於正規表示式中擷取群組數量的數字,或對正規表示式中不存在的名稱的參照。這種反向參照可以用三種不同的方式處理。Delphi、Perl、Ruby、PHP、R、Boost、std::regex、XPath 和 Tcl 會用空字串取代無效反向參照。Java、XRegExp、PCRE2 和 Python 會將它們視為語法錯誤。JavaScript(沒有 XRegExp)和 .NET 將它們視為字面文字。

對非參與擷取群組的反向參照

非參與擷取群組是完全未參與比對嘗試的群組。這與比對空字串的群組不同。a(b?)c 中的群組總是參與比對。其內容是選用的,但群組本身不是選用的。a(b)?c 中的群組是選用的。當正規表示式比對 abc 時,它會參與,但當正規表示式比對 ac 時,它不會參與。

在大部分應用程式中,替換字串中對比對空字串的群組或未參與的群組的回朔參考之間沒有差異。兩者都會替換為空字串。兩個例外是 Python 和 PCRE2。它們允許替換字串中對選用擷取群組的回朔參考。但如果擷取群組碰巧未參與其中一個正規表示式比對,搜尋和取代會在 PCRE2 中傳回錯誤碼。在 Python 3.4 及之前版本中,相同的情況會引發例外。Python 3.5 不再引發例外。

對編號最高的群組的回朔參考

在 Delphi 中,$+ 會插入實際參與比對的編號最高的群組比對到的文字。在 Perl 5.18 中,變數 $+ 包含相同的文字。當 (a)(b)|(c)(d) 比對 ab 時,$+ 會替換為 b。當相同的正規表示式比對 cd 時,$+ 會插入 d。\+ 在 Delphi 和 Ruby 中執行相同的動作。

在 .NET、VBScript 和 Boost 中,$+ 會插入編號最高的群組比對到的文字,不論它是否參與比對。如果它沒有參與,則不會插入任何東西。在 Perl 5.16 及之前版本中,變數 $+ 包含相同的文字。當 (a)(b)|(c)(d) 比對 ab 時,$+ 會替換為空字串。當相同的正規表示式比對 cd 時,$+ 會插入 d。

Boost 1.42 新增了它自己發明的其他語法,用於編號最高的群組的任何意義。$^N、$LAST_SUBMATCH_RESULT 和 ${^LAST_SUBMATCH_RESULT} 都會插入實際參與比對的編號最高的群組比對到的文字。$LAST_PAREN_MATCH 和 ${^LAST_PAREN_MATCH} 都會插入編號最高的群組比對到的文字,不論它是否參與比對。

Numbered Backreferences
  • 简
  • 繁
  • En
About Regular Expressions » Replacement Strings Tutorial » Numbered Backreferences

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

Numbered Backreferences

If your regular expression has named or numbered capturing groups, then you can reinsert the text matched by any of those capturing groups in the replacement text. Your replacement text can reference as many groups as you like, and can even reference the same group more than once. This makes it possible to rearrange the text matched by a regular expression in many different ways. As a simple example, the regex \*(\w+)\* matches a single word between asterisks, storing the word in the first (and only) capturing group. The replacement text <b>\1</b> replaces each regex match with the text stored by the capturing group between bold tags. Effectively, this search-and-replace replaces the asterisks with bold tags, leaving the word between the asterisks in place. This technique using backreferences is important to understand. Replacing *word* as a whole with <b>word</b> is far easier and far more efficient than trying to come up with a way to correctly replace the asterisks separately.

The \1 syntax for backreferences in the replacement text is borrowed from the syntax for backreferences in the regular expression. \1 through \9 are supported by Delphi, Perl (though deprecated), Python, Ruby, PHP, R, Boost, and Tcl. Double-digit backreferences \10 through \99 are supported by Delphi, Python, and Boost. If there are not enough capturing groups in the regex for the double-digit backreference to be valid, then all these flavors treat \10 through \99 as a single-digit backreference followed by a literal digit. The flavors that support single-digit backreferences but not double-digit backreferences also do this.

$1 through $99 for single-digit and double-digit backreferences are supported by Delphi, .NET, Java, JavaScript, VBScript, PCRE2, PHP, Boost, std::regex, and XPath. These are also the variables that hold text matched by capturing groups in Perl. If there are not enough capturing groups in the regex for a double-digit backreference to be valid, then $10 through $99 are treated as a single-digit backreference followed by a literal digit by all these flavors except .NET, Perl, PCRE2, and std::regex..

Putting curly braces around the digit ${1} isolates the digit from any literal digits that follow. This works in Delphi, .NET, Perl, PCRE2, PHP, Boost, and XRegExp.

Named Backreferences

If your regular expression has named capturing groups, then you should use named backreferences to them in the replacement text. The regex (?'name'group) has one group called “name”. You can reference this group with ${name} in Delphi, .NET, PCRE2, Java 7, and XRegExp. PCRE2 also supports $name without the curly braces. In Perl 5.10 and later you can interpolate the variable $+{name}. Boost too uses $+{name} in replacement strings. ${name} does not work in any version of Perl. $name is unique to PCRE2.

In Python, if you have the regex (?P<name>group) then you can use its match in the replacement text with \g<name>. This syntax also works in Delphi. Python, but not Delphi, also support numbered backreferences using this syntax. In Python this is the only way to have a numbered backreference immediately followed by a literal digit.

PHP and R support named capturing groups and named backreferences in regular expressions. But they do not support named backreferences in replacement texts. You’ll have to use numbered backreferences in the replacement text to reinsert text matched by named groups. To determine the numbers, count the opening parentheses of all capturing groups (named and unnamed) in the regex from left to right.

Backreferences to Non-Existent Capturing Groups

An invalid backreference is a reference to a number greater than the number of capturing groups in the regex or a reference to a name that does not exist in the regex. Such a backreference can be treated in three different ways. Delphi, Perl, Ruby, PHP, R, Boost, std::regex, XPath, and Tcl substitute the empty string for invalid backreferences. Java, XRegExp, PCRE2, and Python treat them as a syntax error. JavaScript (without XRegExp) and .NET treat them as literal text.

Backreferences to Non-Participating Capturing Groups

A non-participating capturing group is a group that did not participate in the match attempt at all. This is different from a group that matched an empty string. The group in a(b?)c always participates in the match. Its contents are optional but the group itself is not optional. The group in a(b)?c is optional. It participates when the regex matches abc, but not when the regex matches ac.

In most applications, there is no difference between a backreference in the replacement string to a group that matched the empty string or a group that did not participate. Both are replaced with an empty string. Two exceptions are Python and PCRE2. They do allow backreferences in the replacement string to optional capturing groups. But the search-and-replace will return an error code in PCRE2 if the capturing group happens not to participate in one of the regex matches. The same situation raises an exception in Python 3.4 and prior. Python 3.5 no longer raises the exception.

Backreference to The Highest-Numbered Group

In Delphi, $+ inserts the text matched by the highest-numbered group that actually participated in the match. In Perl 5.18, the variable $+ holds the same text. When (a)(b)|(c)(d) matches ab, $+ is substituted with b. When the same regex matches cd, $+ inserts d. \+ does the same in Delphi, and Ruby.

In .NET, VBScript, and Boost $+ inserts the text matched by the highest-numbered group, regardless of whether it participated in the match or not. If it didn’t, nothing is inserted. In Perl 5.16 and prior, the variable, the variable $+ holds the same text. When (a)(b)|(c)(d) matches ab, $+ is substituted with the empty string. When the same regex matches cd, $+ inserts d.

Boost 1.42 added additional syntax of its own invention for either meaning of highest-numbered group. $^N, $LAST_SUBMATCH_RESULT, and ${^LAST_SUBMATCH_RESULT} all insert the text matched by the highest-numbered group that actually participated in the match. $LAST_PAREN_MATCH and ${^LAST_PAREN_MATCH} both insert the text matched by the highest-numbered group regardless of whether participated in the match.

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