Java正则表达式后视组没有明显的最大长度错误 [英] Java regex look-behind group does not have obvious maximum length error

查看:82
本文介绍了Java正则表达式后视组没有明显的最大长度错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道java正则表达式不支持不同长度的后视,并且以下内容应该导致错误

I know that java regex does not support varying length look-behinds, and that the following should cause an error

(?<=(not exceeding|no((\\w|\\s)*)more than))xxxx

但是*用固定长度说明符替换*

but when the * is replaced with a fixed length specifier as such

(?<=(not exceeding|no((\\w|\\s){0,30})more than))xxxx

它仍然失败。这是为什么?

it still fails. Why is this?

推荐答案

Java Lookbehind是臭名昭着的错误



所以你以为Java不支持无限外观?

Java Lookbehind is Notoriously Buggy

So you thought Java did not support infinite lookbehind?

但是下面的模式会编译!

But the following pattern will compile!

(?<=\d+)\w+

...虽然在Match All中它会产生意想不到的结果(参见演示)。

...though in a Match All it will yield unexpected results (see demo).

另一方面,你可以成功地使用这个其他的无限外观(我在这个问题

On the other hand, you can with success use this other infinite lookbehind (which I found with great surprise on this question)

(?<=\\G\\d+,\\d+,\\d+),

分割此字符串: 0,123,45,6789,4,5,3,4,6000

它将正确输出(参见< a href =http://ideone.com/ux7Wap =nofollow noreferrer>在线演示):

It will correctly output (see the online demo):

0,123,45
6789,4,5
3,4,6000

这次结果是你所期望的。

This time the results are what you expect.

但是如果你调整正则表达式来获得对而不是三元组,使用 (?< = \\G \\\\ +,\\ d +),,这次它不会拆分(参见演示)。

But if you tweak the regex the slightest bit to obtain pairs instead of triplets, with (?<=\\G\\d+,\\d+),, this time it will not split (see the demo).


Java lookbehind是臭名昭着的错误。了解这一点,我建议你
不要浪费时间试图理解为什么会这样做
没有记录。

前一段时间让我得出这个结论的决定性词语来自Jan Goyvaerts,他是 The的共同作者。 Regex Cookbook 和一个创建了一个极好的正则表达式引擎的arch-regex-guru,需要在阳光下保持大多数正则表达式的调试工具RegexBuddy:

The decisive words that drove me to this conclusion some time ago are those from Jan Goyvaerts, who is a co-author of The Regex Cookbook and an arch-regex-guru who has created a terrific regex engine and needs to stay on top of most regex flavors under the sun for his debugging tool RegexBuddy:


Java在其lookbehind实现中有许多错误。在Java 6中修复了一些(但不是全部b $ b)。

Java has a number of bugs in its lookbehind implementation. Some (but not all) of those were fixed in Java 6.

这篇关于Java正则表达式后视组没有明显的最大长度错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆