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

加载更多搜索结果...

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

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

选用项目

问号使正则表达式中的前一个标记为选用。 colou?r 比对 colour 和 color。问号称为量词。

你可以使用括号将多个标记群组在一起,并在闭括号后加上问号,让多个标记为选用。例如: Nov(ember)? 比对 Nov 和 November。

您可以通过包含多个问号来撰写一个符合多种替代方案的正则表达式。 Feb(ruary)? 23(rd)? 符合 February 23rd、February 23、Feb 23rd 和 Feb 23。

您也可以使用大括号来让某些东西变成选配。 colou{0,1}r 和 colou?r 相同。 POSIX BRE 和 GNU BRE 不支持这两种语法。这些类型需要反斜线来赋予大括号其特殊意义:colou\{0,1\}r。

重要的正则表达式概念:贪婪

问号是本教程介绍的第一个贪婪的元字符。问号给予正则表达式引擎两个选择:尝试符合问号所套用的部分,或不尝试符合它。引擎总是会尝试符合该部分。只有当这会导致整个正则表达式失败时,引擎才会尝试忽略问号所套用的部分。

效果是,如果您将正则表达式 Feb 23(rd)? 套用于字符串 Today is Feb 23rd, 2003,符合的结果永远是 Feb 23rd,而不是 Feb 23。您可以在第一个问号后加上第二个问号,让问号变成懒惰(即关闭贪婪)。

关于其他 重复 操作符的讨论,有更多关于贪婪和懒惰量词的详细信息。

深入了解正则表达式引擎

让我们将正则表达式 colou?r 套用于字符串 The colonel likes the color green。

正则表达式中的第一个符号是 字面 c。它成功匹配的第一个位置是 colonel 中的 c。引擎继续运作,发现 o 匹配 o,l 匹配 l,另一个 o 匹配 o。然后,引擎检查 u 是否匹配 n。这失败了。然而,问号告诉正则表达式引擎,不匹配 u 是可以接受的。因此,引擎跳到下一个正则表达式符号:r。但这也无法匹配 n。现在,引擎只能得出结论,整个正则表达式无法从 colonel 中的 c 开始匹配。因此,引擎重新开始尝试将 c 匹配到 colonel 中的第一个 o。

经过一系列失败后,c 匹配 color 中的 c,o、l 和 o 匹配后面的字符。现在,引擎检查 u 是否匹配 r。这失败了。再次:没问题。问号允许引擎继续使用 r。这匹配 r,引擎报告正则表达式成功匹配我们字符串中的 color。

選用項目
  • 简
  • 繁
  • En
關於正規表示式 » 正規表示式教學 » 選用項目

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

選用項目

問號使正規表示式中的前一個標記為選用。 colou?r 比對 colour 和 color。問號稱為量詞。

你可以使用括號將多個標記群組在一起,並在閉括號後加上問號,讓多個標記為選用。例如: Nov(ember)? 比對 Nov 和 November。

您可以透過包含多個問號來撰寫一個符合多種替代方案的正規表示式。 Feb(ruary)? 23(rd)? 符合 February 23rd、February 23、Feb 23rd 和 Feb 23。

您也可以使用大括號來讓某些東西變成選配。 colou{0,1}r 和 colou?r 相同。 POSIX BRE 和 GNU BRE 不支援這兩種語法。這些類型需要反斜線來賦予大括號其特殊意義:colou\{0,1\}r。

重要的正規表示式概念:貪婪

問號是本教學課程介紹的第一個貪婪的元字元。問號給予正規表示式引擎兩個選擇:嘗試符合問號所套用的部分,或不嘗試符合它。引擎總是會嘗試符合該部分。只有當這會導致整個正規表示式失敗時,引擎才會嘗試忽略問號所套用的部分。

效果是,如果您將正規表示式 Feb 23(rd)? 套用於字串 Today is Feb 23rd, 2003,符合的結果永遠是 Feb 23rd,而不是 Feb 23。您可以在第一個問號後加上第二個問號,讓問號變成懶惰(即關閉貪婪)。

關於其他 重複 運算子的討論,有更多關於貪婪和懶惰量詞的詳細資訊。

深入了解正規表示式引擎

讓我們將正規表示式 colou?r 套用於字串 The colonel likes the color green。

正規表示式中的第一個符號是 字面 c。它成功匹配的第一個位置是 colonel 中的 c。引擎繼續運作,發現 o 匹配 o,l 匹配 l,另一個 o 匹配 o。然後,引擎檢查 u 是否匹配 n。這失敗了。然而,問號告訴正規表示式引擎,不匹配 u 是可以接受的。因此,引擎跳到下一個正規表示式符號:r。但這也無法匹配 n。現在,引擎只能得出結論,整個正規表示式無法從 colonel 中的 c 開始匹配。因此,引擎重新開始嘗試將 c 匹配到 colonel 中的第一個 o。

經過一系列失敗後,c 匹配 color 中的 c,o、l 和 o 匹配後面的字元。現在,引擎檢查 u 是否匹配 r。這失敗了。再次:沒問題。問號允許引擎繼續使用 r。這匹配 r,引擎報告正規表示式成功匹配我們字串中的 color。

Optional Items
  • 简
  • 繁
  • En
About Regular Expressions » Regular Expressions Tutorial » Optional Items

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

Optional Items

The question mark makes the preceding token in the regular expression optional. colou?r matches both colour and color. The question mark is called a quantifier.

You can make several tokens optional by grouping them together using parentheses, and placing the question mark after the closing parenthesis. E.g.: Nov(ember)? matches Nov and November.

You can write a regular expression that matches many alternatives by including more than one question mark. Feb(ruary)? 23(rd)? matches February 23rd, February 23, Feb 23rd and Feb 23.

You can also use curly braces to make something optional. colou{0,1}r is the same as colou?r. POSIX BRE and GNU BRE do not support either syntax. These flavors require backslashes to give curly braces their special meaning: colou\{0,1\}r.

Important Regex Concept: Greediness

The question mark is the first metacharacter introduced by this tutorial that is greedy. The question mark gives the regex engine two choices: try to match the part the question mark applies to, or do not try to match it. The engine always tries to match that part. Only if this causes the entire regular expression to fail, will the engine try ignoring the part the question mark applies to.

The effect is that if you apply the regex Feb 23(rd)? to the string Today is Feb 23rd, 2003, the match is always Feb 23rd and not Feb 23. You can make the question mark lazy (i.e. turn off the greediness) by putting a second question mark after the first.

The discussion about the other repetition operators has more details on greedy and lazy quantifiers.

Looking Inside The Regex Engine

Let’s apply the regular expression colou?r to the string The colonel likes the color green.

The first token in the regex is the literal c. The first position where it matches successfully is the c in colonel. The engine continues, and finds that o matches o, l matches l and another o matches o. Then the engine checks whether u matches n. This fails. However, the question mark tells the regex engine that failing to match u is acceptable. Therefore, the engine skips ahead to the next regex token: r. But this fails to match n as well. Now, the engine can only conclude that the entire regular expression cannot be matched starting at the c in colonel. Therefore, the engine starts again trying to match c to the first o in colonel.

After a series of failures, c matches the c in color, and o, l and o match the following characters. Now the engine checks whether u matches r. This fails. Again: no problem. The question mark allows the engine to continue with r. This matches r and the engine reports that the regex successfully matched color in our string.

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