java.lang.StackOverflowError while using a RegEx to Parse big strings

Unfortunately, Java’s builtin regex support has problems with regexes containing repetitive alternative paths (that is, (A|B)*). This is compiled into a recursive call, which results in a StackOverflow error when used on a very large string.

A possible solution is to rewrite your regex to not use a repititive alternative, but if your goal is to tokenize a string on semicolons, you don’t need a complex regex at all really, just use String.split() with a simple ";" as the argument.

Leave a Comment