Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
    Bookmark Topic Watch Topic
  • New Topic

Infinite recurssion when using Regex to extract column names from SQL  RSS feed

ver sd
Posts: 4
  • Mark post as helpful
  • send pies
  • Report post to moderator
I am trying to get all the columns within a SQL query (including the sub selects). When the code hits matcher.find(). i get the following exception:

Exception in thread "main" java.lang.StackOverflowError
at java.util.regex.Pattern$Branch.match( 4530)
at java.util.regex.Pattern$GroupHead.match(Pattern.ja va:4570)

I am not sure where in the regex is causing the inifinite recursion.
Can anyone shed light on this?

String regex = "^select(([^\\(]|\\([^\\(]+\\))*)from";
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE);
Matcher matcher = pattern.matcher(sql);
if (matcher.find()) {
// Get all groups for this match
for (int i = 0; i <= matcher.groupCount(); i++) {
String groupStr =;

Any input on this is helpful.
Thanks in advance.
Ilja Preuss
Posts: 14112
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please don't post the same question in multiple forums. It creates duplicate conversations and thereby wastes the time of the people trying to help you. See

It is sorta covered in the JavaRanch Style Guide.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!