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

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2024年3月5日
类别
  • 正则表达式
标签
无限递归
  • 简
  • 繁
  • En
关于正则表达式 » 正则表达式教程 » 无限递归

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

无限递归

例如 (?R)?z 或 a?(?R)?z 或 a|(?R)z 这些正则表达式在没有任何必须在递归之前比对的项目下使用递归,可能会导致无限递归。如果正则表达式引擎在未前进文本的情况下到达递归,那么下一个递归将再次在未前进文本的情况下到达递归。对于第一个正则表达式,这会在比对尝试开始时立即发生。对于其他两个,这会在没有更多字母 a 可以比对时发生。

Boost 1.64 将前两个正则表达式视为语法错误,因为它们总是会导致无限递归。它们允许第三个正则表达式,因为它可以比对 a。Ruby 1.9 和更新版本、所有版本的 PCRE,以及 PCRE2 10.20 和更早版本将所有三种形式的潜在无限递归视为语法错误。Perl、PCRE2 10.21 和更新版本,以及 Boost 1.63 和更早版本允许所有三种形式。

循环无限子常式调用

子常式调用也可能导致无限递归。所有版本都以处理整个正则表达式潜在无限递归的方式来处理 ((?1)?z) 或 (a?(?1)?z) 或 (a|(?1)z) 中的潜在无限递归。

但是,如果它们调用的群组有另一个子常式调用,调用第一个子常式调用的父群组,那么本身并非递归的子常式调用最终可能会变成递归。当子常式调用被迫绕圈圈时,也会导致无限递归。在编译正则表达式时侦测这种循环调用比检查直接无限递归更复杂。只有 Ruby 1.9 及更新版本能够侦测这种情况并将其视为语法错误。所有其他版本都允许这些正则表达式。

错误和崩溃

当发生无限递归时,无论是直接递归还是子常式调用绕圈圈,Perl 和 PCRE2 都将其视为比对错误,并中止整个比对尝试。Boost 1.64 通过不尝试递归并假装递归失败来处理这个问题。如果递归是可选的,那么 Boost 1.64 可能会找到其他版本会掷回错误的比对。

当发生无限递归时,Boost 1.63 和更早版本,以及 PCRE 8.12 和更早版本会崩溃。这也影响了 Delphi(最高版本为 XE6)和 PHP(最高版本为 5.4.8),因为它们基于较旧的 PCRE 版本。

无限递归

一个正则表达式,例如 a(?R)z,具有非选项的递归标记,且没有 备选方案(没有相同的递归),会导致无穷递归。此类正则表达式永远找不到匹配项。当 a 匹配时,正则表达式引擎会尝试递归。如果它可以匹配另一个 a,则必须再次尝试递归。最后,a 将用完要匹配的字母。然后递归失败。由于它不是选项,因此正则表达式无法匹配。

编译您的正则表达式时,Ruby 会侦测到此情况。它们将无穷递归标记为语法错误。Perl、PCRE、PCRE2 和 Boost 侦测不到无穷递归。它们只会运行匹配进程,而找不到任何匹配项。

無限遞迴
  • 简
  • 繁
  • En
關於正規表示式 » 正規表示式教學 » 無限遞迴

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

無限遞迴

例如 (?R)?z 或 a?(?R)?z 或 a|(?R)z 這些正規表示式在沒有任何必須在遞迴之前比對的項目下使用遞迴,可能會導致無限遞迴。如果正規表示式引擎在未前進文字的情況下到達遞迴,那麼下一個遞迴將再次在未前進文字的情況下到達遞迴。對於第一個正規表示式,這會在比對嘗試開始時立即發生。對於其他兩個,這會在沒有更多字母 a 可以比對時發生。

Boost 1.64 將前兩個正規表示式視為語法錯誤,因為它們總是會導致無限遞迴。它們允許第三個正規表示式,因為它可以比對 a。Ruby 1.9 和更新版本、所有版本的 PCRE,以及 PCRE2 10.20 和更早版本將所有三種形式的潛在無限遞迴視為語法錯誤。Perl、PCRE2 10.21 和更新版本,以及 Boost 1.63 和更早版本允許所有三種形式。

循環無限子常式呼叫

子常式呼叫也可能導致無限遞迴。所有版本都以處理整個正規表示式潛在無限遞迴的方式來處理 ((?1)?z) 或 (a?(?1)?z) 或 (a|(?1)z) 中的潛在無限遞迴。

但是,如果它們呼叫的群組有另一個子常式呼叫,呼叫第一個子常式呼叫的父群組,那麼本身並非遞迴的子常式呼叫最終可能會變成遞迴。當子常式呼叫被迫繞圈圈時,也會導致無限遞迴。在編譯正規表示式時偵測這種循環呼叫比檢查直接無限遞迴更複雜。只有 Ruby 1.9 及更新版本能夠偵測這種情況並將其視為語法錯誤。所有其他版本都允許這些正規表示式。

錯誤和崩潰

當發生無限遞迴時,無論是直接遞迴還是子常式呼叫繞圈圈,Perl 和 PCRE2 都將其視為比對錯誤,並中止整個比對嘗試。Boost 1.64 透過不嘗試遞迴並假裝遞迴失敗來處理這個問題。如果遞迴是可選的,那麼 Boost 1.64 可能會找到其他版本會擲回錯誤的比對。

當發生無限遞迴時,Boost 1.63 和更早版本,以及 PCRE 8.12 和更早版本會崩潰。這也影響了 Delphi(最高版本為 XE6)和 PHP(最高版本為 5.4.8),因為它們基於較舊的 PCRE 版本。

無限遞迴

一個正則表示式,例如 a(?R)z,具有非選項的遞迴標記,且沒有 備選方案(沒有相同的遞迴),會導致無窮遞迴。此類正則表示式永遠找不到匹配項。當 a 匹配時,正則表示式引擎會嘗試遞迴。如果它可以匹配另一個 a,則必須再次嘗試遞迴。最後,a 將用完要匹配的字母。然後遞迴失敗。由於它不是選項,因此正則表示式無法匹配。

編譯您的正則表示式時,Ruby 會偵測到此情況。它們將無窮遞迴標記為語法錯誤。Perl、PCRE、PCRE2 和 Boost 偵測不到無窮遞迴。它們只會執行匹配程序,而找不到任何匹配項。

Infinite Recursion
  • 简
  • 繁
  • En
About Regular Expressions » Regular Expressions Tutorial » Infinite Recursion

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

Infinite Recursion

Regular expressions such as (?R)?z or a?(?R)?z or a|(?R)z that use recursion without having anything that must be matched in front of the recursion can result in infinite recursion. If the regex engine reaches the recursion without having advanced through the text then the next recursion will again reach the recursion without having advanced through the text. With the first regex this happens immediately at the start of the match attempt. With the other two this happens as soon as there are no further letters a to be matched.

Boost 1.64 treat the first two regexes as a syntax error because they always lead to infinite recursion. They allow the third regex because that one can match a. Ruby 1.9 and later, all versions of PCRE, and PCRE2 10.20 and prior treat all three forms of potential infinite recursion as a syntax error. Perl, PCRE2 10.21 and later, and Boost 1.63 and prior allow all three forms.

Circular Infinite Subroutine Calls

Subroutine calls can also lead to infinite recursion. All flavors handle the potentially infinite recursion in ((?1)?z) or (a?(?1)?z) or (a|(?1)z) in the same way as they handle potentially infinite recursion of the entire regex.

But subroutine calls that are not recursive by themselves may end up being recursive if the group they call has another subroutine call that calls a parent group of the first subroutine call. When subroutine calls are forced to go around in a circle that too leads to infinite recursion. Detecting such circular calls when compiling a regex is more complicated than checking for straight infinite recursion. Only Ruby 1.9 and later are able to detect this and treat it as a syntax error. All other flavors allow these regexes.

Errors and Crashes

When infinite recursion does occur, whether it’s straight recursion or subroutine calls going in circles, Perl and PCRE2 treat it as a matching error that aborts the entire match attempt. Boost 1.64 handles this by not attempting the recursion and acting as if the recursion failed. If the recursion is optional then Boost 1.64 may find matches where other flavors throw errors.

Boost 1.63 and prior and PCRE 8.12 and prior crash when infinite recursion occurs. This also affects Delphi up to version XE6 and PHP up to version 5.4.8 as they are based on older PCRE versions.

Endless Recursion

A regex such as a(?R)z that has a recursion token that is not optional and is not have an alternative without the same recursion leads to endless recursion. Such a regular expression can never find a match. When a matches the regex engine attempts the recursion. If it can match another a then it has to attempt the recursion again. Eventually a will run out of letters to match. The recursion then fails. Because it’s not optional the regex fails to match.

Ruby detect this situation when compiling your regular expression. They flag endless recursion as a syntax error. Perl, PCRE, PCRE2, and Boost do not detect endless recursion. They simply go through the matching process which finds no matches.

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