^* 但节点中的相同表-6ren">
gpt4 book ai didi

java - Java 接受无效的正则表达式。这是 Java 错误还是对期望的错误解释

转载 作者:行者123 更新时间:2023-11-29 04:09:10 26 4
gpt4 key购买 nike

根据 several,此模式不是有效的正则表达式websites

groovy:000> java.util.regex.Pattern.compile("^*");
===> ^*

但节点中的相同表达式正确理解了这一点:

$ node
> new RegExp('^*')
SyntaxError: Invalid regular expression: /^*/: Nothing to repeat

谁在这里? Java,节点/互联网?或者,我是否只是期望从 Java 库中得到一些我不应该得到的东西

最佳答案

我会说指向正则表达式测试工具的链接是错误的(在 PCRE 意义上)。我认为这是因为 JS 实现以不同方式处理这些匹配(参见:https://github.com/gskinner/regexr/issues/28)

请注意,regexr 和 regex101 都接受 ^()*(^)*。此外,Perl v5.18.2 没有问题:运行 echo "ubar"| perl -ne "s/^*/F/; print;" 从我的终端没有警告或错误,并将打印 Fubar

PCRE 规范是这样说的:

It is possible to construct infinite loops by following a subpattern that can match no characters with a quantifier that has no upper limit, for example:

(a?)*

Earlier versions of Perl and PCRE used to give an error at compile time for such patterns. However, because there are cases where this can be useful, such patterns are now accepted, but if any repetition of the subpattern does in fact match no characters, the loop is forcibly broken.

-- https://www.pcre.org/original/doc/html/pcrepattern.html

因此,匹配无限数量的零宽度匹配项(如 ^* 所做的那样)已被规范接受。

关于java - Java 接受无效的正则表达式。这是 Java 错误还是对期望的错误解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56136372/

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