gpt4 book ai didi

regex - 非匹配组的目的是什么

转载 作者:行者123 更新时间:2023-12-05 03:15:16 25 4
gpt4 key购买 nike

我正在阅读 Groovy 教程,他们讨论了如何通过使用 ?: 引导组来创建不匹配的组。这样,该组就不会出现在匹配器上。我不明白的是为什么你会想明确地说不匹配这个组。不把它放在一个组里不是更简单吗?

最佳答案

?: 用于分组但是当你不想捕获它们时。这对于代码的简洁很有用,有时甚至是非常必要的。这有助于在匹配后不存储我们不需要的东西,从而节省空间。

它们还主要与 | 运算符一起使用。

The alternation operator has the lowest precedence of all regex operators. That is, it tells the regex engine to match either everything to the left of the vertical bar, or everything to the right of the vertical bar. If you want to limit the reach of the alternation, you need to use parentheses for grouping. (http://www.regular-expressions.info/alternation.html).

在这种情况下,您不能不将它们放在一个组中就离开它们。您将需要在许多常用的正则表达式中使用交替运算符,例如电子邮件、url 等。希望对您有所帮助。

/(?:http|ftp):\/\/([^\/\r\n]+)(\/[^\r\n]*)?/g是 JavaScript 中的示例 URL 正则表达式,它需要交替运算符并需要分组。如果不分组,匹配将只是所有 http url 的 http

关于regex - 非匹配组的目的是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19719910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com