简体   繁体   中英

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).

Is there a way to handle regular expressions more defensively in Java, like

  • having a non-recursive mode for regex
  • throwing a catchable exception for those regular expressions (instead of a java.lang.Stackoverflow error),

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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