Manikanta Reddy
Manikanta Reddy

Reputation: 857

Split String Using comma but ignore comma if it is brackets or quotes

I've seen many examples, but I am not getting the expected result.

Given a String:

"manikanta, Santhosh, ramakrishna(mani, santhosh), tester"

I would like to get the String array as follows:

manikanta,
Santhosh,
ramakrishna(mani, santhosh),
tester

I tried the following regex (got from another example):

"(\".*?\"|[^\",\\s]+)(?=\\s*,|\\s*$)"

Upvotes: 4

Views: 3395

Answers (3)

Bohemian
Bohemian

Reputation: 424983

This does this trick:

String[] parts = input.split(", (?![^(]*\\))");

which employs a negative lookahead to assert that the next bracket char is not a close bracket, and produces:

manikanta
Santhosh
ramakrishna(mani, santhosh)
tester

The desired output as per your question keeps the trailing commas, which I assume is an oversight, but if you really do want to keep the commas:

String[] parts = input.split("(?<=,) (?![^(]*\\))");

which produces the same, but with the trailing commas intact:

manikanta,
Santhosh,
ramakrishna(mani, santhosh),
tester

Upvotes: 5

Stanislav
Stanislav

Reputation: 28106

Suppose, we can split with whitespaces (due to your example), then you can try this regex \s+(?=([^\)]*\()|([^\)\(]*$)) like:

String str = "manikanta, Santhosh, ramakrishna(mani, santhosh), ramakrishna(mani, santhosh), tester";
String[] ar = str.split("\\s+(?=([^\\)]*\\()|([^\\)\\(]*$))");

Where:

\s+ any number of whitespaces

(?=...) positive lookahead, means that after current position must be the string, that matches to ([^\\)]*\\() or | to ([^\\)\\(]*$)

([^\\)]*\\() ignores whitespaces inside the ( and )

([^\\)\\(]*$)) all whitespaces, if they are not followed by ( and ), here is used to split a part with the tester word

Upvotes: 0

Sebastian
Sebastian

Reputation: 415

As I stated in my comment to the question this problem may be impossible to solve by regular expressions.

The following code (java) gives a hint what to do:

private void parse() {
    String string = null;
    char[] chars = string.toCharArray();
    List<String> parts = new ArrayList<String>();

    boolean split = true;
    int lastEnd = 0;
    for (int i = 0; i < chars.length; i++) {
        char c = chars[i];
        switch (c) {
        case '(':
            split = false;
            break;
        case ')':
            split = true;
            break;
        }
        if (split && c == ',') {
            parts.add(string.substring(lastEnd, i - 1));
            lastEnd = i++;
        }
    }
}

Note that the code lacks some checks for constraints (provided string is null, array borders, ...).

Upvotes: -1

Related Questions