Java Regex引擎崩溃 [英] Java Regex Engine Crashing

查看:102
本文介绍了Java Regex引擎崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正则表达式 - ([^ =](\\\\ * [\\w - 。] *)* $)

测试字符串 - paginationInput.entriesPerPage = 5

Java Regex引擎崩溃/采取年龄(> 2分钟)寻找比赛。以下测试输入不是这种情况:

Java Regex Engine Crashing / Taking Ages (> 2mins) finding a match. This is not the case for the following test inputs:

paginationInput=5

paginationInput.entries=5

我的要求是获取右侧的字符串 of = 并将其替换为某些内容。除了上面提到的输入外,上面的模式做得很好。

My requirement is to get hold of the String on the right-hand side of = and replace it with something. The above pattern is doing it fine except for the input mentioned above.

我想了解错误的原因以及如何根据我的要求优化Regex以避免其他特殊情况。

I want to understand why the error and how can I optimize the Regex for my requirement so as to avoid other peculiar cases.

推荐答案

您可以使用后面的外观来确保您的字符串从<$ c $之后的字符处开始c> = :

You can use a look behind to make sure your string starts at the character after the =:

(?<=\\=)([\\s\\w\\-.]*)$

至于为什么它崩溃了,这是该组周围的第二个 * 。我不确定你为什么需要它,因为这听起来像你要求:

As for why it is crashing, it's the second * around the group. I'm not sure why you need that, since that sounds like you are asking for :


  • 单个字符,除了等于

  • 然后0或更多重复以下组:

    • 任何数量的空格

    • 然后任意数量的单词字符,短划线或点

    无论如何,取出 * ,它不再永远旋转,但我仍然会使用更具体的正则表达式看看背后的情况。

    Anyway, take out that *, and it doesn't spin forever anymore, but I'd still go for the more specific regex using the look behind.

    另外,我不知道你是怎么用的,但为什么你有 $ 在那里?然后你只能匹配字符串中的最后一个(如果你有多个)。看起来你会更好地预测新行结束:(?= \\ n | $)

    Also, I don't know how you are using this, but why did you have the $ in there? Then you can only match the last one in the string (if you have more than one). It seems like you'd be better off with a look-ahead to the new line or the end: (?=\\n|$)

    [修改]:更新以下评论。

    [Edit]: Update per comment below.

    这篇关于Java Regex引擎崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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