1

I have the following sql queries I'd like to be able to group in one regular expression:

CREATE INDEX blah_idx ON blah (id ASC)

CREATE INDEX blah2_idx ON blah2 (foo ASC, id DEC)

I'd like to be able to use a java regex to group these so that I get:

blah_idx, blah, id, ASC

blah2_idx, blah2, foo, ASC, id, DEC

I can get the first with CREATE INDEX (\\\w+) ON (\\\w+) \\((\w+) (\w+) \\) but I'd like to be able to also group the second but I can't see to define \\((\w+) (\w+) \\) to match repeatedly.

Is this even possible?

2 Answers 2

1

I left out some parentheses for readability. And spaces can be \\s+ or *.

"CREATE INDEX \\w+ ON \\w+ \\((\\w+ (ASC|DESC)(, \\w+ (ASC|DEC))*))\\)"
                              1     2        23       4       43 21   

Nested groups ( ( ) ) are allowed and are numbered from left to right. For retrieval see the javadoc.

    final String[] sqls = {
        "CREATE INDEX blah_idx ON blah (id ASC)",
        "CREATE INDEX blah2_idx ON blah2 (foo ASC, id DEC)",
        "CREATE INDEX blah2_idx ON blah2 (foo ASC, id DEC, name ASC)",
    };

    final Pattern createIndexPattern = Pattern.compile(
      "CREATE INDEX (\\w+) ON (\\w+) \\(((\\w+) (ASC|DESC)(, (\\w+) (ASC|DEC))*)\\)");
    for (String sql : sqls) {
        System.out.println("SQL: " + sql);
        Matcher m = createIndexPattern.matcher(sql);
        if (!m.matches()) {
            System.out.println("No match!");
        } else {
            System.out.println("Match!");
            int groupCount = m.groupCount();
            for (int groupI = 1; groupI <= groupCount; ++groupI) {
                System.out.printf("[%d] %s%n", groupI, m.group(groupI));
            }
            String[] fieldsWithOrdering = m.group(3).split(",\\s*");
            System.out.println(Arrays.toString(fieldsWithOrdering));
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

oh that's very cool. I think I can work with this, since I know I have at most 3 sets of that foo ASC, section. Thank you!
1

Reminds me of a question I once asked:

How to match nested function invocations (bracket pairs) using a regular expression (recursive?)

Not possible in most Regexp languages including Java unfortunately.

4 Comments

could have posted this as a comment I suppose.
ah bummer... I guess I'll just match the outer, and then match the stuff inside the parens individually?
Usually have to resort to doing that kind of thing with regexes - at least it keeps it simpler for those reading your code.
@tophersmith116, or do it properly and parse it instead. @ Steve, most popular regex flavors support recursion (Perl/PCRE/etc), or similarly powerful constructs (balancing groups in .NET).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.