简体   繁体   English

没有堆栈溢出的Java正则表达式解析器

[英]Java regex parser without stack overflow

Handling a regex with java.util.regex leads to a stack overflow for large strings and complicated regex (especially with | in it). 处理与正则表达式java.util.regex导致堆栈溢出的大型字符串和正则表达式复杂(尤其是具有|它)。

Is there a way to handle regular expressions more defensively in Java, like 有没有办法在Java中更具防御性地处理正则表达式,比如

  • having a non-recursive mode for regex 具有正则表达式的非递归模式
  • throwing a catchable exception for those regular expressions (instead of a java.lang.Stackoverflow error), 为这些正则表达式抛出一个可捕获的异常(而不是java.lang.Stackoverflow错误),

or any other mechanism that does not kill my program or put it in an unrecoverable state? 或任何其他机制不会杀死我的程序或使其处于不可恢复的状态?

The StackOverflowError can be caught and handled just as any exception. StackOverflowError可以像任何异常一样被捕获和处理。 Errors signals serious problems that you normally should not catch, but in this case you know what it is and you need to handle it. 错误表示您通常不应该捕获的严重问题,但在这种情况下,您知道它是什么,您需要处理它。 Just catch it and handle the situation (or re-throw a custom exception). 抓住它并处理情况(或重新抛出自定义异常)。

You might also want to consider using the -Xss command line flag to increase your stack size. 您可能还需要考虑使用-Xss命令行标志来增加堆栈大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM