如何在正则表达式中使用非捕获组,即(?:),它们有什么好处?


当前回答

我是一名JavaScript开发人员,将尝试解释其与JavaScript相关的意义。

考虑一个场景,你想将猫与动物相匹配当你想要匹配猫和动物时,两者之间应该有一个平衡点。

 // this will ignore "is" as that's is what we want
"cat is animal".match(/(cat)(?: is )(animal)/) ;
result ["cat is animal", "cat", "animal"]

 // using lookahead pattern it will match only "cat" we can
 // use lookahead but the problem is we can not give anything
 // at the back of lookahead pattern
"cat is animal".match(/cat(?= is animal)/) ;
result ["cat"]

 //so I gave another grouping parenthesis for animal
 // in lookahead pattern to match animal as well
"cat is animal".match(/(cat)(?= is (animal))/) ;
result ["cat", "cat", "animal"]

 // we got extra cat in above example so removing another grouping
"cat is animal".match(/cat(?= is (animal))/) ;
result ["cat", "animal"]

其他回答

tl;dr非捕获组,顾名思义,是正则表达式中不希望包含在匹配中的部分,以及?:是一种将组定义为非捕获的方法。

假设你有一个电子邮件地址example@example.com.以下正则表达式将创建两个组,id部分和@example.com部分。(\p{Alpha}*[a-z])(@example.com)。为了简单起见,我们提取了包括@字符在内的整个域名。

现在让我们假设,您只需要地址的id部分。您要做的是获取匹配结果的第一个组,在正则表达式中用()包围,这样做的方法是使用非捕获组语法,即?:。因此,正则表达式(\p{Alpha}*[a-z])(?:@example.com)将只返回电子邮件的id部分。

(?:…)充当一个组(…),但不捕获匹配的数据。它确实比标准的捕获组效率高得多。当你想对某个东西进行分组,但以后不需要重用它时,就可以使用它了@托托

历史动机:

非捕获组的存在可以用括号来解释。

考虑表达式(a|b)c和a|bc,由于串联优先于|,这些表达式分别表示两种不同的语言({ac,bc}和{a,bc})。

然而,括号也用作匹配组(如其他答案所解释的…)。

当您想有括号但不想捕获子表达式时,可以使用NON-CAPTURING GROUPS。在示例中,(?:a|b)c

打开您的Google Chrome devTools,然后单击Console选项卡:并键入以下内容:

"Peace".match(/(\w)(\w)(\w)/)

运行它,您将看到:

["Pea", "P", "e", "a", index: 0, input: "Peace", groups: undefined]

JavaScript RegExp引擎捕获三个组,索引为1、2、3的项。现在使用非捕获标记来查看结果。

"Peace".match(/(?:\w)(\w)(\w)/)

结果是:

["Pea", "e", "a", index: 0, input: "Peace", groups: undefined]

这是显而易见的非捕获组。

它非常简单,我们可以通过简单的日期示例来理解,假设日期被提到为2019年1月1日或2019年5月2日或任何其他日期,并且我们只想将其转换为dd/mm/yyyy格式,那么我们不需要月份的名称,即1月或2月,因此为了捕获数字部分,而不是(可选)后缀,您可以使用非捕获组。

所以正则表达式将是,

([0-9]+)(?:January|February)?

就这么简单。