Forum Stats

  • 3,855,318 Users
  • 2,264,499 Discussions
  • 7,905,968 Comments

Discussions

Stack overflows when alternation is used in the Java regular expression

User_WA972
User_WA972 Member Posts: 1 Green Ribbon
edited Apr 11, 2022 8:37AM in New To Java

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)*+