简体   繁体   English

正则表达式:数字与对捕获组的反向引用

[英]Regex: a number vs. a backreference to a capture group

I've been studying regular expressions, and I'm scratching my head on this one.我一直在研究正则表达式,我对这个正在挠头。 On this page ( https://www.regular-expressions.info/conditional.html ) I see that, in a conditional regex, a reference to a numbered backreference is just a number.在此页面 ( https://www.regular-expressions.info/conditional.html ) 上,我看到,在条件正则表达式中,对编号反向引用的引用只是一个数字。 For example,例如,

(a)?b(?(1)c|d)

How does regex know that we aren't supposed to match the number "1" instead of the backreference to the 1st capture group?正则表达式如何知道我们不应该匹配数字“1”而不是对第一个捕获组的反向引用? Previously in the lessons I had learned that a backreference would be escaped, such as \\1, \\2, etc.以前在课程中我了解到会转义反向引用,例如 \\1、\\2 等。

As per the regex tutorial you're following:根据正则表达式教程,您正在关注:

A special construct (?ifthen|else) allows you to create conditional regular expressions .一个特殊的结构(?ifthen|else)允许您创建条件正则表达式 If the if part evaluates to true, then the regex engine will attempt to match the then part.如果 if 部分的计算结果为 true,则正则表达式引擎将尝试匹配 then 部分。 Otherwise, the else part is attempted instead.否则,将尝试使用 else 部分。 The syntax consists of a pair of parentheses.语法由一对括号组成。 The opening bracket must be followed by a question mark , immediately followed by the if part, immediately followed by the then part.左括号后面必须跟一个问号,紧跟在 if 部分,紧跟在 then 部分。 This part can be followed by a vertical bar and the else part.这部分后面可以跟一个竖线和 else 部分。 You may omit the else part, and the vertical bar with it.你可以省略 else 部分,以及它的竖线。

Alternatively, you can check in the if part whether a capturing group has taken part in the match thus far.或者,您可以在 if 部分检查到目前为止是否有捕获组参与了比赛。 Place the number of the capturing group inside parentheses , and use that as the if part.将捕获组的编号放在括号内,并将其用作 if 部分。

Your second question is this:你的第二个问题是这样的:

RegEx Demo of \\b(a)?b(?(1)c|d)\\b \\b(a)?b(?(1)c|d)\\b表达式演示

Note that I have added word boundary to avoid matching string like abd partially.请注意,我添加了单词边界以避免部分匹配像abd这样的字符串。

What if someone actually wanted to match the literal 1 this way?如果有人真的想以这种方式匹配文字 1 怎么办?

valid input: 1c or d invalid input: 1d有效输入: 1cd无效输入: 1d

That would be:那将是:

\b(1)?(?(1)c|d)\b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM