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

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2024年3月5日
类别
  • 正则表达式
标签
相对后向参照
  • 简
  • 繁
  • En
关于正则表达式 » 正则表达式教程 » 相对后向参照

正则表达式教程
简介
目录
特殊字符
不可打印字符
正则表达式引擎内部
字符类别
字符类别减法
字符类别交集
简写字符类别
点
锚定
字词边界
交替
可选项目
重复
群组和截取
后向参照
后向参照,第 2 部分
命名组
相对后向参照
分支重设群组
自由间距和注解
Unicode
模式修改器
原子组
独占量词
前瞻和后顾
环顾,第 2 部分
将文本排除在比对之外
条件
平衡组
递归
子常式
无限递归
递归和量词
递归和截取
递归和后向参照
递归和回溯
POSIX 方括号表达式
零长度比对
继续比对
本网站的其他内容
简介
正则表达式快速入门
正则表达式教程
替换字符串教程
应用程序和语言
正则表达式范例
正则表达式参考
替换字符串参考

相对后向参照

某些应用程序支持相对反向引用。这些应用程序使用负数来参照反向引用之前的群组。若要找出相对反向引用所参照的群组,请取反向引用的绝对数字,并从反向引用开始,从右至左计算 (已命名或未命名) 捕获组的打开括号数量。因此,(a)(b)(c)\k<-1> 符合 abcc,而 (a)(b)(c)\k<-3> 符合 abca。如果反向引用位于捕获组内,则您也需要计算该捕获组的打开括号。因此,(a)(b)(c\k<-2>) 符合 abcb。 (a)(b)(c\k<-1>) 无法符合或会产生错误,视您的应用程序是否允许 嵌套反向引用 而定。

嵌套反向引用的语法差异很大。它通常是 已命名反向引用 语法的延伸。Ruby 1.9 及更新版本支持 \k<-1> 和 \k'-1'。虽然这看起来像是 .NET 已命名截取的语法,但 .NET 本身不支持相对反向引用。

Perl 5.10、PCRE 7.0、PHP 5.2.2 和 R 支持 \g{-1} 和 \g-1。

Boost 从 Boost 1.42 开始支持 Perl 语法。Boost 从 Boost 1.47 开始添加 Ruby 语法。为了让事情更复杂,Boost 1.47 允许这些变体相乘。Boost 1.47 及更新版本允许使用 \g 或 \k 以及大括号、尖括号或引号来指定相对反向引用。这产生六种变体,加上 \g-1,总共有七种变体。这让 Boost 与 Ruby、PCRE、PHP 和 R 产生冲突,后者将使用尖括号或引号的 \g 和负数视为 相对子常式调用。

相對後向參照
  • 简
  • 繁
  • En
關於正規表示式 » 正規表示式教學 » 相對後向參照

正規表示式教學
簡介
目錄
特殊字元
不可列印字元
正規表示式引擎內部
字元類別
字元類別減法
字元類別交集
簡寫字元類別
點
錨定
字詞邊界
交替
可選項目
重複
群組和擷取
後向參照
後向參照,第 2 部分
命名群組
相對後向參照
分支重設群組
自由間距和註解
Unicode
模式修改器
原子群組
獨佔量詞
前瞻和後顧
環顧,第 2 部分
將文字排除在比對之外
條件
平衡群組
遞迴
子常式
無限遞迴
遞迴和量詞
遞迴和擷取
遞迴和後向參照
遞迴和回溯
POSIX 方括號表示式
零長度比對
繼續比對
本網站的其他內容
簡介
正規表示式快速入門
正規表示式教學
替換字串教學
應用程式和語言
正規表示式範例
正規表示式參考
替換字串參考

相對後向參照

某些應用程式支援相對反向參照。這些應用程式使用負數來參照反向參照之前的群組。若要找出相對反向參照所參照的群組,請取反向參照的絕對數字,並從反向參照開始,從右至左計算 (已命名或未命名) 擷取群組的開啟括號數量。因此,(a)(b)(c)\k<-1> 符合 abcc,而 (a)(b)(c)\k<-3> 符合 abca。如果反向參照位於擷取群組內,則您也需要計算該擷取群組的開啟括號。因此,(a)(b)(c\k<-2>) 符合 abcb。 (a)(b)(c\k<-1>) 無法符合或會產生錯誤,視您的應用程式是否允許 巢狀反向參照 而定。

巢狀反向參照的語法差異很大。它通常是 已命名反向參照 語法的延伸。Ruby 1.9 及更新版本支援 \k<-1> 和 \k'-1'。雖然這看起來像是 .NET 已命名擷取的語法,但 .NET 本身不支援相對反向參照。

Perl 5.10、PCRE 7.0、PHP 5.2.2 和 R 支援 \g{-1} 和 \g-1。

Boost 從 Boost 1.42 開始支援 Perl 語法。Boost 從 Boost 1.47 開始新增 Ruby 語法。為了讓事情更複雜,Boost 1.47 允許這些變體相乘。Boost 1.47 及更新版本允許使用 \g 或 \k 以及大括號、尖括號或引號來指定相對反向參照。這產生六種變體,加上 \g-1,總共有七種變體。這讓 Boost 與 Ruby、PCRE、PHP 和 R 產生衝突,後者將使用尖括號或引號的 \g 和負數視為 相對子常式呼叫。

Relative Backreferences
  • 简
  • 繁
  • En
About Regular Expressions » Regular Expressions Tutorial » Relative Backreferences

Regex Tutorial
Introduction
Table of Contents
Special Characters
Non-Printable Characters
Regex Engine Internals
Character Classes
Character Class Subtraction
Character Class Intersection
Shorthand Character Classes
Dot
Anchors
Word Boundaries
Alternation
Optional Items
Repetition
Grouping & Capturing
Backreferences
Backreferences, part 2
Named Groups
Relative Backreferences
Branch Reset Groups
Free-Spacing & Comments
Unicode
Mode Modifiers
Atomic Grouping
Possessive Quantifiers
Lookahead & Lookbehind
Lookaround, part 2
Keep Text out of The Match
Conditionals
Balancing Groups
Recursion
Subroutines
Infinite Recursion
Recursion & Quantifiers
Recursion & Capturing
Recursion & Backreferences
Recursion & Backtracking
POSIX Bracket Expressions
Zero-Length Matches
Continuing Matches
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

Relative Backreferences

Some applications support relative backreferences. These use a negative number to reference a group preceding the backreference. To find the group that the relative backreference refers to, take the absolute number of the backreference and count that many opening parentheses of (named or unnamed) capturing groups starting at the backreference and going from right to left through the regex. So (a)(b)(c)\k<-1> matches abcc and (a)(b)(c)\k<-3> matches abca. If the backreference is inside a capturing group, then you also need to count that capturing group’s opening parenthesis. So (a)(b)(c\k<-2>) matches abcb. (a)(b)(c\k<-1>) either fails to match or is an error depending on whether your application allows nested backreferences.

The syntax for nested backreferences varies widely. It is generally an extension of the syntax for named backreferences. Ruby 1.9 and later support \k<-1> and \k'-1'. Though this looks like the .NET syntax for named capture, .NET itself does not support relative backreferences.

Perl 5.10, PCRE 7.0, PHP 5.2.2, and R support \g{-1} and \g-1.

Boost supports the Perl syntax starting with Boost 1.42. Boost adds the Ruby syntax starting with Boost 1.47. To complicate matters, Boost 1.47 allowed these variants to multiply. Boost 1.47 and later allow relative backreferences to be specified with \g or \k and with curly braces, angle brackets, or quotes. That makes six variations plus \g-1 for a total of seven variations. This puts Boost in conflict with Ruby, PCRE, PHP and R which treat \g with angle brackets or quotes and a negative number as a relative subroutine call.

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