Skip to Main Content

Stack overflows when alternation is used in the Java regular expression

User_WA972Apr 11 2022 — edited Apr 11 2022

use of large input string causes the stack overflow, when the characters to be matched are added in alternation
input = "xyxyxyxy...";
regExpression = (x|y)*;
input.matches(regExpression);
Need to know what is the reason for the stack overflow in java? Found one related article that Java regular expression uses recursive calls to implement backtracking.
https://rules.sonarsource.com/java/tag/regex/RSPEC-5998
This gets resolved, when atomic group is created like
regExpression = (**?>**x|y)*
or
[xy]*
or
(x|y)*+

Comments
Post Details
Added on Apr 11 2022
0 comments
64 views