Java正则表达式错误 - Look-behind组没有明显的最大长度
我收到此错误:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了避免此错误,您应该将
+
替换为{0,10}
之类的区域:To avoid this error, you should replace
+
with a region like{0,10}
:Java 不支持向后查找中的可变长度。
在这种情况下,似乎您可以轻松地忽略它(假设您的整个输入是一个单词):
两个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):
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
复制自此处
Copied from Here