Java正则表达式错误 - Look-behind组没有明显的最大长度

发布于 2024-12-06 11:55:32 字数 332 浏览 1 评论 0原文

我收到此错误:

java.util.regex.PatternSyntaxException: Look-behind group does not have an
    obvious maximum length near index 22
([a-z])(?!.*\1)(?<!\1.+)([a-z])(?!.*\2)(?<!\2.+)(.)(\3)(.)(\5)
                      ^

我尝试匹配 COFFEE,但不匹配 BOBBEE

我使用的是java 1.6。

I get this error:

java.util.regex.PatternSyntaxException: Look-behind group does not have an
    obvious maximum length near index 22
([a-z])(?!.*\1)(?<!\1.+)([a-z])(?!.*\2)(?<!\2.+)(.)(\3)(.)(\5)
                      ^

I'm trying to match COFFEE, but not BOBBEE.

I'm using java 1.6.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏日浅笑〃 2024-12-13 11:55:32

为了避免此错误,您应该将 + 替换为 {0,10} 之类的区域:

([a-z])(?!.*\1)(?<!\1.{0,10})([a-z])(?!.*\2)(?<!\2.{0,10})(.)(\3)(.)(\5)

To avoid this error, you should replace + with a region like {0,10}:

([a-z])(?!.*\1)(?<!\1.{0,10})([a-z])(?!.*\2)(?<!\2.{0,10})(.)(\3)(.)(\5)
电影里的梦 2024-12-13 11:55:32

Java 不支持向后查找中的可变长度。
在这种情况下,似乎您可以轻松地忽略它(假设您的整个输入是一个单词):

([a-z])(?!.*\1)([a-z])(?!.*\2)(.)(\3)(.)(\5)

两个lookbehinds都不会添加任何内容:第一个断言至少有两个字符,而您只有一个字符,第二个检查第二个字符是否不同从第一个开始,它已经被 (?!.*\1) 覆盖了。

工作示例:http://regexr.com?2up96

Java doesn't support variable length in look behind.
In this case, it seems you can easily ignore it (assuming your entire input is one word):

([a-z])(?!.*\1)([a-z])(?!.*\2)(.)(\3)(.)(\5)

Both lookbehinds do not add anything: the first asserts at least two characters where you only had one, and the second checks the second character is different from the first, which was already covered by (?!.*\1).

Working example: http://regexr.com?2up96

_蜘蛛 2024-12-13 11:55:32

Java 更进一步,允许有限重复。您仍然不能使用星号或加号,但可以使用问号和大括号并指定 max 参数。 Java 确定后向查找的最小和最大可能长度。
正则表达式(?中的lookbehind有6种可能的长度。它的长度可以在 7 到 11 个字符之间。当 Java(版本 6 或更高版本)尝试匹配lookbehind 时,它首先后退字符串中的最小字符数(本例中为7),然后像往常一样从左到右评估lookbehind 内的正则表达式。如果失败,Java 会再后退一个字符并重试。如果回溯继续失败,Java 会继续后退,直到回溯匹配或已回退最大字符数(本例中为 11)。当向后查找的可能长度数量增加时,重复后退主题字符串会降低性能。请记住这一点。不要选择任意大的最大重复次数来解决后向查找中缺乏无限量词的问题。 Java 4 和 5 存在一些错误,这些错误会导致使用交替或变量量词的后向查找在某些情况下应该成功时却失败。这些错误已在 Java 6 中修复。


复制自此处

Java takes things a step further by allowing finite repetition. You still cannot use the star or plus, but you can use the question mark and the curly braces with the max parameter specified. Java determines the minimum and maximum possible lengths of the lookbehind.
The lookbehind in the regex (?<!ab{2,4}c{3,5}d)test has 6 possible lengths. It can be between 7 to 11 characters long. When Java (version 6 or later) tries to match the lookbehind, it first steps back the minimum number of characters (7 in this example) in the string and then evaluates the regex inside the lookbehind as usual, from left to right. If it fails, Java steps back one more character and tries again. If the lookbehind continues to fail, Java continues to step back until the lookbehind either matches or it has stepped back the maximum number of characters (11 in this example). This repeated stepping back through the subject string kills performance when the number of possible lengths of the lookbehind grows. Keep this in mind. Don't choose an arbitrarily large maximum number of repetitions to work around the lack of infinite quantifiers inside lookbehind. Java 4 and 5 have bugs that cause lookbehind with alternation or variable quantifiers to fail when it should succeed in some situations. These bugs were fixed in Java 6.

Copied from Here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文