使用括号进行分组和截取使用括号进行分组和截取使用括号进行分组和截取使用括号进行分组和截取
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2024年3月5日
类别
  • 正则表达式
标签
使用括号进行分组和截取
  • 简
  • 繁
  • En
关于正则表达式 » 正则表达式教程 » 使用括号进行分组和截取

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

使用括号进行分组和截取

将正则表达式的一部分放在圆括号或括号中,您可以将正则表达式的该部分分组在一起。这允许您将量词应用于整个群组,或将交替限制在正则表达式的部分。

只有括号可用于分组。方括号定义字符类别,而大括号则由具有特定限制的量词使用。

括号创建编号捕获组

除了将正则表达式的一部分分组在一起,括号还会创建一个编号的捕获组。它会保存括号内正则表达式部分所配对的字符串部分。

正则表达式 Set(Value)? 会配对 Set 或 SetValue。在第一个情况下,第一个(也是唯一的)捕获组会保持为空。在第二个情况下,第一个捕获组会配对 Value。

非捕获组

如果您不需要群组截取其配对,您可以将此正则表达式优化为 Set(?:Value)?。问号和开括号后的冒号是创建非捕获组的语法。开括号后的问号与正则表达式结尾的问号无关。最后一个问号是量词,会让前一个代码 可选。此量词不能出现在开括号后,因为在群组开始时没有任何东西可以选择。因此,即使这在起初可能会令人困惑,问号作为将代码设为可选的操作符,以及问号作为非捕获组语法的一部分,两者之间没有歧义。还有其他类型的群组会使用 (? 语法搭配冒号以外的其他字符,本教程稍后会说明。

color=(?:red|green|blue) 是另一个具有非捕获组的正则表达式。此正则表达式没有量词。

支持 命名截取 的正则表达式风格通常有选项可以 将所有未命名组转换为非捕获组。

使用捕获组配对的文本

捕获组可以轻松截取正则表达式配对的一部分。您可以通过 反向引用 在正则表达式内重复使用文本。反向引用也可以用于替换字符串。请查看 替换文本教程 以取得详细数据。

使用括號進行分組和擷取
  • 简
  • 繁
  • En
關於正規表示式 » 正規表示式教學 » 使用括號進行分組和擷取

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

使用括號進行分組和擷取

將正規表示式的一部分放在圓括號或括號中,您可以將正規表示式的該部分分組在一起。這允許您將量詞應用於整個群組,或將交替限制在正規表示式的部分。

只有括號可用於分組。方括號定義字元類別,而大括號則由具有特定限制的量詞使用。

括號建立編號擷取群組

除了將正規表示式的一部分分組在一起,括號還會建立一個編號的擷取群組。它會儲存括號內正規表示式部分所配對的字串部分。

正規表示式 Set(Value)? 會配對 Set 或 SetValue。在第一個情況下,第一個(也是唯一的)擷取群組會保持為空。在第二個情況下,第一個擷取群組會配對 Value。

非擷取群組

如果您不需要群組擷取其配對,您可以將此正規表示式最佳化為 Set(?:Value)?。問號和開括號後的冒號是建立非擷取群組的語法。開括號後的問號與正規表示式結尾的問號無關。最後一個問號是量詞,會讓前一個代碼 可選。此量詞不能出現在開括號後,因為在群組開始時沒有任何東西可以選擇。因此,即使這在起初可能會令人困惑,問號作為將代碼設為可選的運算子,以及問號作為非擷取群組語法的一部分,兩者之間沒有歧義。還有其他類型的群組會使用 (? 語法搭配冒號以外的其他字元,本教學課程稍後會說明。

color=(?:red|green|blue) 是另一個具有非擷取群組的正規表示式。此正規表示式沒有量詞。

支援 命名擷取 的正規表示式風格通常有選項可以 將所有未命名群組轉換為非擷取群組。

使用擷取群組配對的文字

擷取群組可以輕鬆擷取正規表示式配對的一部分。您可以透過 反向參照 在正規表示式內重複使用文字。反向參照也可以用於替換字串。請查看 替換文字教學課程 以取得詳細資料。

Use Parentheses for Grouping and Capturing
  • 简
  • 繁
  • En
About Regular Expressions » Regular Expressions Tutorial » Use Parentheses for Grouping and Capturing

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

Use Parentheses for Grouping and Capturing

By placing part of a regular expression inside round brackets or parentheses, you can group that part of the regular expression together. This allows you to apply a quantifier to the entire group or to restrict alternation to part of the regex.

Only parentheses can be used for grouping. Square brackets define a character class, and curly braces are used by a quantifier with specific limits.

Parentheses Create Numbered Capturing Groups

Besides grouping part of a regular expression together, parentheses also create a numbered capturing group. It stores the part of the string matched by the part of the regular expression inside the parentheses.

The regex Set(Value)? matches Set or SetValue. In the first case, the first (and only) capturing group remains empty. In the second case, the first capturing group matches Value.

Non-Capturing Groups

If you do not need the group to capture its match, you can optimize this regular expression into Set(?:Value)?. The question mark and the colon after the opening parenthesis are the syntax that creates a non-capturing group. The question mark after the opening parenthesis is unrelated to the question mark at the end of the regex. The final question mark is the quantifier that makes the previous token optional. This quantifier cannot appear after an opening parenthesis, because there is nothing to be made optional at the start of a group. Therefore, there is no ambiguity between the question mark as an operator to make a token optional and the question mark as part of the syntax for non-capturing groups, even though this may be confusing at first. There are other kinds of groups that use the (? syntax in combination with other characters than the colon that are explained later in this tutorial.

color=(?:red|green|blue) is another regex with a non-capturing group. This regex has no quantifiers.

Regex flavors that support named capture often have an option to turn all unnamed groups into non-capturing groups.

Using Text Matched By Capturing Groups

Capturing groups make it easy to extract part of the regex match. You can reuse the text inside the regular expression via a backreference. Backreferences can also be used in replacement strings. Please check the replacement text tutorial for details.

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