-1

I was trying to split a sting and add it to an string array.

String str1 = "1, two, {three,4,5,{six,7}}, 8, nine,{[ten,{\"11\":\"12\"},{\"thirteen\":14}]},\"fifteen\",16";

Required output in an array is:

1
two
{three,4,5,{six,7}}
8
nine
{[ten,{"11":"12"},{"thirteen":14}]}
fifteen
16

We would need to split this with a split function in java using a regex. Can anyone suggest the regex to fetch this output?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563

3 Answers3

0

If you text does not change its structure, you can write regex especially for this purpose. Things I would do with this:

  • Beat the man who sends you this output
  • Convince him to send it better way and separate each logic part by comma and space
  • Now, you can use regex ,\s to split your string
Adam Ostrožlík
  • 1,256
  • 1
  • 10
  • 16
0

A regex is not the tool you need.

You can try something like this:

public static String[] split(String s) {
    List<String> result = new ArrayList<>();
    StringBuilder buf = new StringBuilder();
    int level = 0;
    boolean inQuotes = false;
    for (char c: s.toCharArray()) {
        switch (c) {
            case ' ':
                if (buf.length() > 0) {
                    buf.append(c);
                }
                break;
            case ',':
                if (level == 0 && !inQuotes) {
                    result.add(buf.toString());
                    buf.setLength(0);
                } else {
                    buf.append(c);
                }
                break;
            case '"':
                buf.append(c);
                inQuotes = !inQuotes;
                break;
            case '[':
            case '{':
                buf.append(c);
                if (!inQuotes) {
                    ++level;
                }
                break;
            case ']':
            case '}':
                buf.append(c);
                if (!inQuotes) {
                    --level;
                }
                break;
            default:
                buf.append(c);
                break;
        }
    }
    if (inQuotes || level > 0) {
        throw new IllegalArgumentException("Syntax error");
    }
    result.add(buf.toString());
    return result.toArray(new String[result.size()]);
}

But it will not get rid of the quotes (BTW: I don't get how the quotes are supposed to be dealt with):

1
two
{[three,4,5,{six,7}]}
8
nine
{[ten,{"11":"12"},{"thirteen":14}]}
"fifteen"
16
Maurice Perry
  • 9,261
  • 2
  • 12
  • 24
0

You can use the following regex to do it -> ,(?![^\\[]*\\])

Check the next code, it splits the String with the above regex. Then it iterates the array (of the String parts) and trims each value, and finally it prints out the array.

public static void main(String[] args) {
        String str1 = "1, two, {[three,4,5,{six,7}]}, 8, nine,{[ten,{\"11\":\"12\"},{\"thirteen\":14}]},\"fifteen\",16";
        String[] stringParts = str1.split(",(?![^\\[]*\\])");

        for (int i=0; i<stringParts.length; i++){
            stringParts[i] = stringParts[i].trim();//remove trailing leading spaces.
        }
        //System out
        for (String s:stringParts){
            System.out.println(s);
        }
    }

Output:

1
two
{[three,4,5,{six,7}]}
8
nine
{[ten,{"11":"12"},{"thirteen":14}]}
"fifteen"
16
Ioannis Barakos
  • 1,319
  • 1
  • 11
  • 16
  • Hi @IoannisBarakos, Thanks for the answer, i have updated the output in 3rd line could you please provide the Regex for it. – greeshma setty Nov 07 '19 at 11:08