替换字符串条件替换字符串条件替换字符串条件替换字符串条件
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2024年3月5日
类别
  • 正则表达式
标签
替换字符串条件
  • 简
  • 繁
  • En
关于正则表达式 » 替换字符串教程 » 替换字符串条件

替换文本教学
简介
字符
不可打印字符
配对文本
反向引用
配对内容
大小写转换
条件
本网站更多信息
简介
正则表达式快速入门
正则表达式教程
替换字符串教程
应用程序和语言
正则表达式范例
正则表达式参考
替换字符串参考

替换字符串条件

替换字符串条件让您可以在特定捕获组参与配对时使用一个替换,而在该捕获组未参与配对时使用另一个替换。此功能受 Boost 和 PCRE2 支持。Boost 和 PCRE2 各自发明了自己的语法。

若要在 Boost 中使用条件,您需要将 regex_constants::format_all 传递至 regex_replace。若要在 PCRE2 中使用条件,您需要将 PCRE2_SUBSTITUTE_EXTENDED 传递至 pcre2_substitute。

Boost 替换字符串条件

Boost 的语法是 (?1匹配:不匹配),其中 1 是 1 到 99 之间的数字,用于参照编号的捕获组。匹配 用于替换捕获组参与其中的匹配项。不匹配 用于群组未参与其中的匹配项。冒号 : 区分这两个部分。如果您想要在 匹配 部分中使用文本冒号,则需要使用反斜线对其进行转义。如果您想要在条件式中的任何位置使用文本右括号,则也需要使用反斜线对其进行转义。

括号将条件式与替换字符串的其余部分分隔开来。开始(?1匹配:不匹配)结束 在群组参与时替换为 开始匹配结束,在群组未参与时替换为 开始不匹配结束。如果替换中的条件式后面没有任何内容,Boost 允许您省略括号。因此,?1匹配:不匹配 与 (?1匹配:不匹配) 相同。

配对和未配对的部分可以是空白。如果未配对的部分是空白,则可以省略冒号。因此,当群组参与时,matched: 和 matched 会取代为 matched。当群组未参与时,它们会将配对内容替换为空白。

您可以在 matched 和 unmatched 中使用完整的替换字符串语法。这表示您可以在其他条件式内嵌套条件式。因此,当两个群组都参与时,(?1one(?2two):(?2two:none)) 会取代为 onetwo;当群组 1 或 2 参与而另一个未参与时,会取代为 one 或 two;当两个群组都未参与时,会取代为 none。使用 Boost ?1one(?2two):?2two:none 会运行完全相同的动作,但会省略不必要的括号。

如果问号后有两个数字,但没有足够的捕获组让两个数字的条件式有效,则仅会使用第一个数字作为条件式,而第二个数字会是文本。因此,当正则表达式中少于 12 个捕获组时,(?12matched) 会在捕获组 1 参与配对时取代为 2matched。

Boost 将参照不存在的群组编号的条件式视为对从未参与配对的群组的条件式。因此,当正则表达式中少于 12 个捕获组时,(?12twelve:not twelve) 会永远取代为 not twelve。

您可以通过在数字周围加上大括号来避免单数字元和双数字元条件式之间的歧义。(?{1}1:0) 在群组 1 参与时取代为 1,在群组 1 未参与时取代为 0,即使正则表达式中有 11 个以上的捕获组也是如此。(?{12}twelve:not twelve) 永远是参考群组 12 的条件式,即使正则表达式中少于 12 个群组(这可能会使条件式无效)。

使用大括号的语法也允许您通过名称来参考命名捕获组。(?{name}matched:unmatched) 在群组「name」参与比对时取代为 matched,在群组「name」未参与比对时取代为 unmatched。如果群组不存在,Boost 会将参考不存在群组名称的条件式视为文本。因此,(?{nonexisting}matched:unmatched) 使用 ?{nonexisting}matched:unmatched 作为文本替换。

PCRE2 替换字符串条件式

PCRE2 的语法为 ${1:+matched:unmatched},其中 1 是介于 1 到 99 之间的数字,用来参考编号的捕获组。如果您的正则表达式包含命名捕获组,则您可以通过名称在条件式中参考它们:${name:+matched:unmatched}。

matched 用于替换捕获组参与的比对。unmatched 用于捕获组未参与的比对。:+ 将群组编号或名称与条件式的第一部分分隔开来。第二个冒号分隔两个部分。如果您要在 matched 部分中使用文本冒号,则需要使用反斜线对其进行转义。如果您要在条件式的任何位置使用文本闭合大括号,则也需要使用反斜线对其进行转义。除了用于开始条件式的 :+ 之外,加号没有任何特殊意义,因此不需要对其进行转义。

您可以在 matched 和 unmatched 中使用完整的替换字符串语法。这表示您可以在其他条件式内嵌套条件式。因此 ${1:+one${2:+two}:${2:+two:none}} 会在两个群组都参与时替换为 onetwo,在群组 1 或 2 参与而另一个没有参与时替换为 one 或 two,在两个群组都没有参与时替换为 none。

${1:-unmatched} 和 ${name:-unmatched} 是 ${1:+${1}:unmatched} 和 ${name:+${name}:unmatched} 的简写。如果群组参与比对,它们会插入群组截取的文本。如果群组没有参与,它们会插入 unmatched。使用这个语法时,:- 会将群组编号或名称与条件式的内容区隔开来。条件式只有一个部分,其中冒号和减号没有特殊意义。

PCRE2 会将参照不存在捕获组的条件式视为错误。

转义问号、冒号、括号和大括号

如上所述,您需要使用反斜线转义您想要在条件式的 matched 部分中当作字面值使用的冒号。您还需要使用反斜线转义条件式内的字面值右括号 (Boost) 或大括号 (PCRE2)。

在支持条件式的替换字符串风格中,您可以使用反斜线转义冒号、括号、大括号,甚至问号,以确保它们在替换字符串中的任何位置都被解释为字面值。但通常不需要这样做。

冒号在 unmatched 部分或条件式外部没有任何特殊意义。因此您不需要在那里转义它。如果问号后面没有数字或大括号,它没有任何特殊意义。在 PCRE2 中,它永远没有特殊意义。因此您只需要在您想要在 Boost 中使用字面值问号后接字面值数字或大括号作为替换时,才需要使用反斜线转义问号。

Boost 总是使用括号进行分组。未转义的打开括号总是打开一个群组。群组可以嵌套。未转义的关闭括号总是关闭一个群组。未转义的关闭括号如果没有匹配的打开括号,实际上会截断替换字符串。因此 Boost 要求您始终使用反斜线转义字面括号。

替換字串條件
  • 简
  • 繁
  • En
關於正規表示式 » 替換字串教學 » 替換字串條件

替換文字教學
簡介
字元
不可列印字元
配對文字
反向參照
配對內容
大小寫轉換
條件
本網站更多資訊
簡介
正規表示式快速入門
正規表示式教學
替換字串教學
應用程式和語言
正規表示式範例
正規表示式參考
替換字串參考

替換字串條件

替換字串條件讓您可以在特定擷取群組參與配對時使用一個替換,而在該擷取群組未參與配對時使用另一個替換。此功能受 Boost 和 PCRE2 支援。Boost 和 PCRE2 各自發明了自己的語法。

若要在 Boost 中使用條件,您需要將 regex_constants::format_all 傳遞至 regex_replace。若要在 PCRE2 中使用條件,您需要將 PCRE2_SUBSTITUTE_EXTENDED 傳遞至 pcre2_substitute。

Boost 替換字串條件

Boost 的語法是 (?1匹配:不匹配),其中 1 是 1 到 99 之間的數字,用於參照編號的擷取群組。匹配 用於替換擷取群組參與其中的匹配項。不匹配 用於群組未參與其中的匹配項。冒號 : 區分這兩個部分。如果您想要在 匹配 部分中使用文字冒號,則需要使用反斜線對其進行跳脫。如果您想要在條件式中的任何位置使用文字右括號,則也需要使用反斜線對其進行跳脫。

括號將條件式與替換字串的其餘部分分隔開來。開始(?1匹配:不匹配)結束 在群組參與時替換為 開始匹配結束,在群組未參與時替換為 開始不匹配結束。如果替換中的條件式後面沒有任何內容,Boost 允許您省略括號。因此,?1匹配:不匹配 與 (?1匹配:不匹配) 相同。

配對和未配對的部分可以是空白。如果未配對的部分是空白,則可以省略冒號。因此,當群組參與時,matched: 和 matched 會取代為 matched。當群組未參與時,它們會將配對內容替換為空白。

您可以在 matched 和 unmatched 中使用完整的替換字串語法。這表示您可以在其他條件式內嵌套條件式。因此,當兩個群組都參與時,(?1one(?2two):(?2two:none)) 會取代為 onetwo;當群組 1 或 2 參與而另一個未參與時,會取代為 one 或 two;當兩個群組都未參與時,會取代為 none。使用 Boost ?1one(?2two):?2two:none 會執行完全相同的動作,但會省略不必要的括號。

如果問號後有兩個數字,但沒有足夠的擷取群組讓兩個數字的條件式有效,則僅會使用第一個數字作為條件式,而第二個數字會是文字。因此,當正規表示式中少於 12 個擷取群組時,(?12matched) 會在擷取群組 1 參與配對時取代為 2matched。

Boost 將參照不存在的群組編號的條件式視為對從未參與配對的群組的條件式。因此,當正規表示式中少於 12 個擷取群組時,(?12twelve:not twelve) 會永遠取代為 not twelve。

您可以透過在數字周圍加上大括號來避免單數位元和雙數位元條件式之間的歧義。(?{1}1:0) 在群組 1 參與時取代為 1,在群組 1 未參與時取代為 0,即使正規表示式中有 11 個以上的擷取群組也是如此。(?{12}twelve:not twelve) 永遠是參考群組 12 的條件式,即使正規表示式中少於 12 個群組(這可能會使條件式無效)。

使用大括號的語法也允許您透過名稱來參考命名擷取群組。(?{name}matched:unmatched) 在群組「name」參與比對時取代為 matched,在群組「name」未參與比對時取代為 unmatched。如果群組不存在,Boost 會將參考不存在群組名稱的條件式視為文字。因此,(?{nonexisting}matched:unmatched) 使用 ?{nonexisting}matched:unmatched 作為文字替換。

PCRE2 替換字串條件式

PCRE2 的語法為 ${1:+matched:unmatched},其中 1 是介於 1 到 99 之間的數字,用來參考編號的擷取群組。如果您的正規表示式包含命名擷取群組,則您可以透過名稱在條件式中參考它們:${name:+matched:unmatched}。

matched 用於替換擷取群組參與的比對。unmatched 用於擷取群組未參與的比對。:+ 將群組編號或名稱與條件式的第一部分分隔開來。第二個冒號分隔兩個部分。如果您要在 matched 部分中使用文字冒號,則需要使用反斜線對其進行跳脫。如果您要在條件式的任何位置使用文字閉合大括號,則也需要使用反斜線對其進行跳脫。除了用於開始條件式的 :+ 之外,加號沒有任何特殊意義,因此不需要對其進行跳脫。

您可以在 matched 和 unmatched 中使用完整的替換字串語法。這表示您可以在其他條件式內嵌套條件式。因此 ${1:+one${2:+two}:${2:+two:none}} 會在兩個群組都參與時替換為 onetwo,在群組 1 或 2 參與而另一個沒有參與時替換為 one 或 two,在兩個群組都沒有參與時替換為 none。

${1:-unmatched} 和 ${name:-unmatched} 是 ${1:+${1}:unmatched} 和 ${name:+${name}:unmatched} 的簡寫。如果群組參與比對,它們會插入群組擷取的文字。如果群組沒有參與,它們會插入 unmatched。使用這個語法時,:- 會將群組編號或名稱與條件式的內容區隔開來。條件式只有一個部分,其中冒號和減號沒有特殊意義。

PCRE2 會將參照不存在擷取群組的條件式視為錯誤。

跳脫問號、冒號、括號和大括號

如上所述,您需要使用反斜線跳脫您想要在條件式的 matched 部分中當作字面值使用的冒號。您還需要使用反斜線跳脫條件式內的字面值右括號 (Boost) 或大括號 (PCRE2)。

在支援條件式的替換字串風格中,您可以使用反斜線跳脫冒號、括號、大括號,甚至問號,以確保它們在替換字串中的任何位置都被解釋為字面值。但通常不需要這樣做。

冒號在 unmatched 部分或條件式外部沒有任何特殊意義。因此您不需要在那裡跳脫它。如果問號後面沒有數字或大括號,它沒有任何特殊意義。在 PCRE2 中,它永遠沒有特殊意義。因此您只需要在您想要在 Boost 中使用字面值問號後接字面值數字或大括號作為替換時,才需要使用反斜線跳脫問號。

Boost 總是使用括號進行分組。未跳脫的開啟括號總是開啟一個群組。群組可以巢狀。未跳脫的關閉括號總是關閉一個群組。未跳脫的關閉括號如果沒有匹配的開啟括號,實際上會截斷替換字串。因此 Boost 要求您始終使用反斜線跳脫字面括號。

Replacement String Conditionals
  • 简
  • 繁
  • En
About Regular Expressions » Replacement Strings Tutorial » Replacement String Conditionals

Replacement Text Tutorial
Introduction
Characters
Non-Printable Characters
Matched Text
Backreferences
Match Context
Case Conversion
Conditionals
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

Replacement String Conditionals

Replacement string conditionals allow you to use one replacement when a particular capturing group participated in the match and another replacement when that capturing group did not participate in the match. They are supported by Boost, and PCRE2. Boost and PCRE2 each invented their own syntax.

For conditionals to work in Boost, you need to pass regex_constants::format_all to regex_replace. For them to work in PCRE2, you need to pass PCRE2_SUBSTITUTE_EXTENDED to pcre2_substitute.

Boost Replacement String Conditionals

Boost’s syntax is (?1matched:unmatched) where 1 is a number between 1 and 99 referencing a numbered capturing group. matched is used as the replacement for matches in which the capturing group participated. unmatched is used for matches in which the group did not participate. The colon : delimits the two parts. If you want a literal colon in the matched part, then you need to escape it with a backslash. If you want a literal closing parenthesis anywhere in the conditional, then you need to escape that with a backslash too.

The parentheses delimit the conditional from the remainder of the replacement string. start(?1matched:unmatched)finish replaces with startmatchedfinish when the group participates and with startunmatchedfinish when it doesn’t. Boost allows you to omit the parentheses if nothing comes after the conditional in the replacement. So ?1matched:unmatched is the same as (?1matched:unmatched).

The matched and unmatched parts can be blank. You can omit the colon if the unmatched part is blank. So (?1matched:) and (?1matched) replace with matched when the group participates. They replace the match with nothing when the group does not participate.

You can use the full replacement string syntax in matched and unmatched. This means you can nest conditionals inside other conditionals. So (?1one(?2two):(?2two:none)) replaces with onetwo when both groups participate, with one or two when group 1 or 2 participates and the other doesn’t, and with none when neither group participates. With Boost ?1one(?2two):?2two:none does exactly the same but omits parentheses that aren’t needed.

If there are two digits after the question mark but not enough capturing groups for a two-digit conditional to be valid, then only the first digit is used for the conditional and the second digit is a literal. So when there are less than 12 capturing groups in the regex, (?12matched) replaces with 2matched when capturing group 1 participates in the match.

Boost treats conditionals that reference a non-existing group number as conditionals to a group that never participates in the match. So (?12twelve:not twelve) always replaces with not twelve when there are fewer than 12 capturing groups in the regex.

You can avoid the ambiguity between single digit and double digit conditionals by placing curly braces around the number. (?{1}1:0) replaces with 1 when group 1 participates and with 0 when it doesn’t, even if there are 11 or more capturing groups in the regex. (?{12}twelve:not twelve) is always a conditional that references group 12, even if there are fewer than 12 groups in the regex (which may make the conditional invalid).

The syntax with curly braces also allows you to reference named capturing groups by their names. (?{name}matched:unmatched) replaces with matched when the group “name” participates in the match and with unmatched when it doesn’t. If the group does not exist, Boost, treats conditionals that reference a non-existing group name as literals. So (?{nonexisting}matched:unmatched) uses ?{nonexisting}matched:unmatched as a literal replacement.

PCRE2 Replacement String Conditional

PCRE2’s syntax is ${1:+matched:unmatched} where 1 is a number between 1 and 99 referencing a numbered capturing group. If your regex contains named capturing groups then you can reference them in a conditional by their name: ${name:+matched:unmatched}.

matched is used as the replacement for matches in which the capturing group participated. unmatched is used for matches in which the group did not participate. :+ delimits the group number or name from the first part of the conditional. The second colon delimits the two parts. If you want a literal colon in the matched part, then you need to escape it with a backslash. If you want a literal closing curly brace anywhere in the conditional, then you need to escape that with a backslash too. Plus signs have no special meaning beyond the :+ that starts the conditional, so they don’t need to be escaped.

You can use the full replacement string syntax in matched and unmatched. This means you can nest conditionals inside other conditionals. So ${1:+one${2:+two}:${2:+two:none}} replaces with onetwo when both groups participate, with one or two when group 1 or 2 participates and the other doesn’t, and with none when neither group participates.

${1:-unmatched} and ${name:-unmatched} are shorthands for ${1:+${1}:unmatched} and ${name:+${name}:unmatched}. They insert the text captured by the group if it participated in the match. They insert unmatched if the group did not participate. When using this syntax, :- delimits the group number or name from the contents of the conditional. The conditional has only one part in which colons and minus signs have no special meaning.

PCRE2 treat conditionals that reference non-existing capturing groups as an error.

Escaping Question Marks, Colons, Parentheses, and Curly Braces

As explained above, you need to use backslashes to escape colons that you want to use as literals when used in the matched part of the conditional. You also need to escape literal closing parentheses (Boost) or curly braces (PCRE2) with backslashes inside conditionals.

In replacement string flavors that support conditionals, you can escape colons, parentheses, curly braces, and even question marks with backslashes to make sure they are interpreted as literals anywhere in the replacement string. But generally there is no need to.

The colon does not have any special meaning in the unmatched part or outside conditionals. So you don’t need to escape it there. The question mark does not have any special meaning if it is not followed by a digit or a curly brace. In PCRE2 it never has a special meaning. So you only need to escape question marks with backslashes if you want to use a literal question mark followed by a literal digit or curly brace as the replacement in Boost.

Boost always uses parentheses for grouping. An unescaped opening parenthesis always opens a group. Groups can be nested. An unescaped closing parenthesis always closes a group. An unescaped closing parenthesis that does not have a matching opening parenthesis effectively truncates the replacement string. So Boost requires you to always escape literal parentheses with backslashes.

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