Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java regex, split on comma only if not in quotes or brackets

Tags:

java

regex

I would like to do a java split via regex. I would like to split my string on every comma when it is NOT in single quotes or brackets. example:

Hello, 'my,',friend,(how ,are, you),(,)


 should give:
    hello
    my,
    friend
    how, are, you
    ,

I tried this:

(?i),(?=([^\'|\(]*\'|\([^\'|\(]*\'|\()*[^\'|\)]*$)

But I can't get it to work (I tested via http://java-regex-tester.appspot.com/)

Any ideas?

like image 782
PoeHaH Avatar asked Dec 04 '25 16:12

PoeHaH


1 Answers

Nested paranthesises can't be split by regex. Its easier to split them manually.

public static List<String> split(String orig) {
    List<String> splitted = new ArrayList<String>();
    int nextingLevel = 0;
    StringBuilder result = new StringBuilder();
    for (char c : orig.toCharArray()) {
        if (c == ',' && nextingLevel == 0) {
            splitted.add(result.toString());
            result.setLength(0);// clean buffer
        } else {
            if (c == '(')
                nextingLevel++;
            if (c == ')')
                nextingLevel--;
            result.append(c);
        }
    }
    // Thanks PoeHah for pointing it out. This adds the last element to it.
    splitted.add(result.toString());
    return splitted;
}

Hope this helps.

like image 65
Sri Harsha Chilakapati Avatar answered Dec 06 '25 06:12

Sri Harsha Chilakapati