简体   繁体   English

正则表达式 (或)在其中

[英]RegEx with | (or) in it

I have a long RegEx that is working, but I have a section at the end that is perplexing me. 我有一个很长的正则表达式正在工作,但是最后我有一个部分让我感到困惑。 I have a scenario where I am parsing some HTML and one of two scenarios can happen. 我有一种情况,我正在解析一些HTML,并且可能发生两种情况之一。 Either the pattern I am searching for ends with a X followed immediately by a single digit or it's a   我正在搜索的模式以X结尾,然后紧跟一个数字,或者是  . Here's the RegEx fragment: 这是RegEx片段:

(X(\d+)| )

As you might have noticed, I don't care about the X or the   您可能已经注意到,我不在乎X或  , I just want to capture the digit if it's there. ,我只想捕获那里的数字。 It appears that in order to use the |, I have to use a capture group. 看来,为了使用|,我必须使用捕获组。 So now I get BOTH X5 AND 5 if that pattern exists. 因此,如果该模式存在,我将同时得到X5和5。 I really just want the digit captured if it's there. 我真的只希望捕获到那里的数字。

Thanks! 谢谢!

To get the effect of grouping, without the effect of capturing, use the (?:...) notation: 要获得分组的效果而没有捕获的效果,请使用(?:...)表示法:

(?:X(\d+)| )

This is equivalent to what you wrote, except that it doesn't create a capture group for X5 , only for 5 . 除了不为X5创建捕获组,而仅为5创建捕获组,这与您编写的内容等效。

(By the way, you say "a single digit", but your regex has \\d+ rather than \\d , so it can actually match multiple digits.) (顺便说一句,您说的是“一位数”,但是您的正则表达式具有\\d+而不是\\d ,因此它实际上可以匹配多个数字。)

您可以使用非捕获组吗?

(?:X(\d+)| )

try 尝试

(?:X(\d+)| )

adding ?: you actually disable the backreference while grouping. 添加?:实际上是在分组时禁用了反向引用。

使用非捕获组:

(?:X(\d+)| )

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

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