使用垂直线或管线符号进行交替使用垂直线或管线符号进行交替使用垂直线或管线符号进行交替使用垂直线或管线符号进行交替
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2024年3月5日
类别
  • 正则表达式
标签
使用垂直线或管线符号进行交替
  • 简
  • 繁
  • En
关于正则表达式 » 正则表达式教程 » 使用垂直线或管线符号进行交替

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

使用垂直线或管线符号进行交替

我已经说明如何使用 字符类别 从几个可能的字符中比对单一字符。交替类似。您可以使用交替从几个可能的正则表达式中比对单一正则表达式。

如果您要搜索文本 cat 或 dog,请使用垂直线或管线符号分隔两个选项:cat|dog。如果您要更多选项,只需扩充清单:cat|dog|mouse|fish。

交替操作符在所有 regex 操作符中具有最低优先级。也就是说,它会指示 regex 引擎比对直线符号左边的所有内容,或直线符号右边的所有内容。如果你想要限制交替的范围,你需要使用括号来进行分组。如果我们想要改善第一个范例,只比对完整的字词,我们需要使用 \b(cat|dog)\b。这会指示 regex 引擎寻找 字词边界,然后寻找 cat 或 dog,最后再寻找另一个字词边界。如果我们省略括号,regex 引擎就会寻找字词边界,然后寻找 cat,或寻找 dog,然后寻找字词边界。

请记住 regex 引擎很急切

我已经说明 regex 引擎很急切。它会在找到有效比对后立即停止搜索。因此,在某些情况下,替代方案的顺序很重要。假设你想要使用 regex 比对编程语言中的函数名称清单:Get、GetValue、Set 或 SetValue。显而易见的解法是 Get|GetValue|Set|SetValue。当字符串为 SetValue 时,让我们看看它是如何运作的。

regex 引擎会从 regex 中的第一个标记 G,以及字符串中的第一个字符 S 开始。比对失败。不过,regex 引擎在开始之前已经研究过整个正则表达式。因此,它知道这个正则表达式使用交替,而且整个 regex 尚未失败。所以它会继续运行第二个选项,也就是 regex 中的第二个 G。比对再次失败。下一个标记是 regex 中的第一个 S。比对成功,而且引擎会继续运行字符串中的下一个字符,以及 regex 中的下一个标记。regex 中的下一个标记是刚才成功比对的 S 之后的 e。 e 比对 e。下一个标记 t 比对 t。

在这个时候,交替中的第三个选项已经成功比对。由于 regex 引擎很急切,它会在其中一个选项成功比对后,就认为整个交替已经成功比对。在此范例中,regex 中没有其他标记在交替之外,所以整个 regex 已经成功比对 SetValue 中的 Set。

与我们的本意相反,正则表达式并未比对整个字符串。有几个解决方案。一个选项是考虑正则表达式引擎的急切性,并变更选项的顺序。如果我们使用 GetValue|Get|SetValue|Set,SetValue 会在 Set 之前尝试,而引擎会比对整个字符串。我们也可以将四个选项合并为两个,并使用 问号 使其中一部分为选用:Get(Value)?|Set(Value)?。由于问号是贪婪的,因此 SetValue 会在 Set 之前尝试。

最佳选项可能是表达我们只想要比对完整的单字。如果字符串为 SetValueFunction,我们不想要比对 Set 或 SetValue。因此,解决方案是 \b(Get|GetValue|Set|SetValue)\b 或 \b(Get(Value)?|Set(Value)?)\b。由于所有选项的结尾都相同,我们可以进一步优化为 \b(Get|Set)(Value)?\b。

文本导向引擎传回最长的比对

交替是 正则表达式导向和文本导向引擎不同的部分。当文本导向引擎尝试在 SetValue 上运行 Get|GetValue|Set|SetValue 时,它会尝试在字符串开头运行正则表达式的所有排列组合。它会有效率地运行,而不会进行任何回溯。它会看到正则表达式可以在字符串开头找到比对,而且比对的文本可以是 Set 或 SetValue。由于文本导向引擎将正则表达式视为一个整体来评估,因此它没有概念一个选项列在另一个选项之前。但它必须选择要传回哪个比对。它总是传回最长的比对,在本例中为 SetValue。

POSIX 要求最长匹配

POSIX 标准 由实作选择文本导向或正则表达式导向引擎。包含反向引用的 BRE 需要使用正则表达式导向引擎评估。但没有反向引用的 BRE 或 ERE 可以使用文本导向引擎评估。但 POSIX 标准确实要求传回最长匹配,即使使用正则表达式导向引擎也是如此。此类引擎不能急于求成。它必须在找到匹配后继续尝试所有替代方案,以找到最长的匹配。当正则表达式包含多个量词或量词与交替的组合时,这可能会导致性能非常差,因为必须尝试所有组合才能找到最长匹配。

Tcl 和 GNU 风格也以这种方式运作。

使用垂直線或管線符號進行交替
  • 简
  • 繁
  • En
關於正規表示式 » 正規表示式教學 » 使用垂直線或管線符號進行交替

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

使用垂直線或管線符號進行交替

我已經說明如何使用 字元類別 從幾個可能的字元中比對單一字元。交替類似。您可以使用交替從幾個可能的正規表示式中比對單一正規表示式。

如果您要搜尋文字 cat 或 dog,請使用垂直線或管線符號分隔兩個選項:cat|dog。如果您要更多選項,只需擴充清單:cat|dog|mouse|fish。

交替運算子在所有 regex 運算子中具有最低優先順序。也就是說,它會指示 regex 引擎比對直線符號左邊的所有內容,或直線符號右邊的所有內容。如果你想要限制交替的範圍,你需要使用括號來進行分組。如果我們想要改善第一個範例,只比對完整的字詞,我們需要使用 \b(cat|dog)\b。這會指示 regex 引擎尋找 字詞邊界,然後尋找 cat 或 dog,最後再尋找另一個字詞邊界。如果我們省略括號,regex 引擎就會尋找字詞邊界,然後尋找 cat,或尋找 dog,然後尋找字詞邊界。

請記住 regex 引擎很急切

我已經說明 regex 引擎很急切。它會在找到有效比對後立即停止搜尋。因此,在某些情況下,替代方案的順序很重要。假設你想要使用 regex 比對程式語言中的函式名稱清單:Get、GetValue、Set 或 SetValue。顯而易見的解法是 Get|GetValue|Set|SetValue。當字串為 SetValue 時,讓我們看看它是如何運作的。

regex 引擎會從 regex 中的第一個標記 G,以及字串中的第一個字元 S 開始。比對失敗。不過,regex 引擎在開始之前已經研究過整個正規表示式。因此,它知道這個正規表示式使用交替,而且整個 regex 尚未失敗。所以它會繼續執行第二個選項,也就是 regex 中的第二個 G。比對再次失敗。下一個標記是 regex 中的第一個 S。比對成功,而且引擎會繼續執行字串中的下一個字元,以及 regex 中的下一個標記。regex 中的下一個標記是剛才成功比對的 S 之後的 e。 e 比對 e。下一個標記 t 比對 t。

在這個時候,交替中的第三個選項已經成功比對。由於 regex 引擎很急切,它會在其中一個選項成功比對後,就認為整個交替已經成功比對。在此範例中,regex 中沒有其他標記在交替之外,所以整個 regex 已經成功比對 SetValue 中的 Set。

與我們的本意相反,正規表示式並未比對整個字串。有幾個解決方案。一個選項是考慮正規表示式引擎的急切性,並變更選項的順序。如果我們使用 GetValue|Get|SetValue|Set,SetValue 會在 Set 之前嘗試,而引擎會比對整個字串。我們也可以將四個選項合併為兩個,並使用 問號 使其中一部分為選用:Get(Value)?|Set(Value)?。由於問號是貪婪的,因此 SetValue 會在 Set 之前嘗試。

最佳選項可能是表達我們只想要比對完整的單字。如果字串為 SetValueFunction,我們不想要比對 Set 或 SetValue。因此,解決方案是 \b(Get|GetValue|Set|SetValue)\b 或 \b(Get(Value)?|Set(Value)?)\b。由於所有選項的結尾都相同,我們可以進一步最佳化為 \b(Get|Set)(Value)?\b。

文字導向引擎傳回最長的比對

交替是 正規表示式導向和文字導向引擎不同的部分。當文字導向引擎嘗試在 SetValue 上執行 Get|GetValue|Set|SetValue 時,它會嘗試在字串開頭執行正規表示式的所有排列組合。它會有效率地執行,而不會進行任何回溯。它會看到正規表示式可以在字串開頭找到比對,而且比對的文字可以是 Set 或 SetValue。由於文字導向引擎將正規表示式視為一個整體來評估,因此它沒有概念一個選項列在另一個選項之前。但它必須選擇要傳回哪個比對。它總是傳回最長的比對,在本例中為 SetValue。

POSIX 要求最長匹配

POSIX 標準 由實作選擇文字導向或正規表示式導向引擎。包含反向參照的 BRE 需要使用正規表示式導向引擎評估。但沒有反向參照的 BRE 或 ERE 可以使用文字導向引擎評估。但 POSIX 標準確實要求傳回最長匹配,即使使用正規表示式導向引擎也是如此。此類引擎不能急於求成。它必須在找到匹配後繼續嘗試所有替代方案,以找到最長的匹配。當正規表示式包含多個量詞或量詞與交替的組合時,這可能會導致效能非常差,因為必須嘗試所有組合才能找到最長匹配。

Tcl 和 GNU 風格也以這種方式運作。

Alternation with The Vertical Bar or Pipe Symbol
  • 简
  • 繁
  • En
About Regular Expressions » Regular Expressions Tutorial » Alternation with The Vertical Bar or Pipe Symbol

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

Alternation with The Vertical Bar or Pipe Symbol

I already explained how you can use character classes to match a single character out of several possible characters. Alternation is similar. You can use alternation to match a single regular expression out of several possible regular expressions.

If you want to search for the literal text cat or dog, separate both options with a vertical bar or pipe symbol: cat|dog. If you want more options, simply expand the list: cat|dog|mouse|fish.

The alternation operator has the lowest precedence of all regex operators. That is, it tells the regex engine to match either everything to the left of the vertical bar, or everything to the right of the vertical bar. If you want to limit the reach of the alternation, you need to use parentheses for grouping. If we want to improve the first example to match whole words only, we would need to use \b(cat|dog)\b. This tells the regex engine to find a word boundary, then either cat or dog, and then another word boundary. If we had omitted the parentheses then the regex engine would have searched for a word boundary followed by cat, or, dog followed by a word boundary.

Remember That The Regex Engine Is Eager

I already explained that the regex engine is eager. It stops searching as soon as it finds a valid match. The consequence is that in certain situations, the order of the alternatives matters. Suppose you want to use a regex to match a list of function names in a programming language: Get, GetValue, Set or SetValue. The obvious solution is Get|GetValue|Set|SetValue. Let’s see how this works out when the string is SetValue.

The regex engine starts at the first token in the regex, G, and at the first character in the string, S. The match fails. However, the regex engine studied the entire regular expression before starting. So it knows that this regular expression uses alternation, and that the entire regex has not failed yet. So it continues with the second option, being the second G in the regex. The match fails again. The next token is the first S in the regex. The match succeeds, and the engine continues with the next character in the string, as well as the next token in the regex. The next token in the regex is the e after the S that just successfully matched. e matches e. The next token, t matches t.

At this point, the third option in the alternation has been successfully matched. Because the regex engine is eager, it considers the entire alternation to have been successfully matched as soon as one of the options has. In this example, there are no other tokens in the regex outside the alternation, so the entire regex has successfully matched Set in SetValue.

Contrary to what we intended, the regex did not match the entire string. There are several solutions. One option is to take into account that the regex engine is eager, and change the order of the options. If we use GetValue|Get|SetValue|Set, SetValue is attempted before Set, and the engine matches the entire string. We could also combine the four options into two and use the question mark to make part of them optional: Get(Value)?|Set(Value)?. Because the question mark is greedy, SetValue is be attempted before Set.

The best option is probably to express the fact that we only want to match complete words. We do not want to match Set or SetValue if the string is SetValueFunction. So the solution is \b(Get|GetValue|Set|SetValue)\b or \b(Get(Value)?|Set(Value)?)\b. Since all options have the same end, we can optimize this further to \b(Get|Set)(Value)?\b.

Text-Directed Engine Returns the Longest Match

Alternation is where regex-directed and text-directed engines differ. When a text-directed engine attempts Get|GetValue|Set|SetValue on SetValue, it tries all permutations of the regex at the start of the string. It does so efficiently, without any backtracking. It sees that the regex can find a match at the start of the string, and that the matched text can be either Set or SetValue. Because the text-directed engine evaluates the regex as a whole, it has no concept of one alternative being listed before another. But it has to make a choice as to which match to return. It always returns the longest match, in this case SetValue.

POSIX Requires The Longest Match

The POSIX standard leaves it up to the implementation to choose a text-directed or regex-directed engine. A BRE that includes backreferences needs to be evaluated using a regex-directed engine. But a BRE without backreferences or an ERE can be evaluated using a text-directed engine. But the POSIX standard does mandate that the longest match be returned, even when a regex-directed engine is used. Such an engine cannot be eager. It has to continue trying all alternatives even after a match is found, in order to find the longest one. This can result in very poor performance when a regex contains multiple quantifiers or a combination of quantifiers and alternation, as all combinations have to be tried to find the longest match.

The Tcl and GNU flavors also work this way.

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