2

Trying to split using comma as a delimiter in the string and add into a string array, but facing problem while spliting the string using regex.

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);
        }

Input String

String str="1, two, {\"\"Customization\"\":{\"\"EMPLOYEEID\"\":\"\"EMPID001\"\",\"\"MANAGER_ID\"\":\"\"MNGID001\"\",\"\"DEPARTMENT\"\":\"\"IT\"\"},\"\"OTHERDETAILS\"\":{\"\"GENDER\"\":\"\"M\"\",\"\"DESIGNATION\"\":\"\"SENIOR\"\",\"\"TEAM\"\":\"\"QA\"\"}}, 8, nine,{{\"COMPANYNAME\":\"XYZ Ind Pvt Ltd\"},{[ten,{\"11\":\"12\"},{\"thirteen\":14}]}},\"fifteen\",16";

Required Output

1
two
{""Customization"":{""EMPLOYEEID"":""EMPID001"",""MANAGER_ID"":""MNGID001"",""DEPARTMENT"":""IT""},""OTHERDETAILS"":{""GENDER"":""M"",""DESIGNATION"":""SENIOR"",""TEAM"":""QA""}}
8
nine
{{"COMPANYNAME":"XYZ Ind Pvt Ltd"},{[ten,{"11":"12"},{"thirteen":14}]}}
fifteen
16
Toto
  • 89,455
  • 62
  • 89
  • 125
Hari Charan
  • 33
  • 1
  • 4
  • Stop right there. You should be using a JSON parser. – Tim Biegeleisen Nov 08 '19 at 06:21
  • @TimBiegeleisen Except that input isn't JSON. Outer level is not a JSON array (missing `[]`) and nested object (third value) has double double-quotes. – Andreas Nov 08 '19 at 07:11
  • 1
    You can't use regex (and hence `split`) to process text with nested structures. Try something else, e.g. iterating the characters of the string yourself, keeping track of `{}` nesting and whether inside a `""` string. – Andreas Nov 08 '19 at 07:14
  • If there's no arbitrary nesting: [`[^\s,}{][^,}{]*|\{(?:[^}{]+|\{(?:[^}{]+|\{[^}{]*\})*\})*\}`](https://regex101.com/r/huPK2p/1) Deeper max nesting needs to be added to the pattern. If there's arbitrary nesting, see [this idea](https://stackoverflow.com/a/47162099/5527985). The pattern could look [like this](https://regex101.com/r/huPK2p/2). But be aware, that this is experimental and really slow. It will break on larger strings :) – bobble bubble Nov 08 '19 at 10:13

1 Answers1

1

This isn't something that regex is designed for. You need to create a parser.

Or you could do something similar to this:

public static void main(String[] args) {
    String str = "1, two, {\"\"Customization\"\":{\"\"EMPLOYEEID\"\":\"\"EMPID001\"\",\"\"MANAGER_ID\"\":\"\"MNGID001\"\",\"\"DEPARTMENT\"\":\"\"IT\"\"},\"\"OTHERDETAILS\"\":{\"\"GENDER\"\":\"\"M\"\",\"\"DESIGNATION\"\":\"\"SENIOR\"\",\"\"TEAM\"\":\"\"QA\"\"}}, 8, nine,{{\"COMPANYNAME\":\"XYZ Ind Pvt Ltd\"},{[ten,{\"11\":\"12\"},{\"thirteen\":14}]}},\"fifteen\",16";
    StringTokenizer st = new StringTokenizer(str, ",", true);
    int bracketCount = 0;
    while (st.hasMoreTokens()) {
        String token = st.nextToken();
        long brackets = token.chars().map(ch -> (ch == '{' ? 1 : (ch == '}' ? -1 : 0))).sum();
        bracketCount += brackets;
        if (bracketCount == 0 && ",".equals(token)) {
            System.out.println("");
        } else {
            System.out.print(token);
        }
    }
}
  • Take the string and split on ,, keeping the delimiter as an output token
  • count the number of open and close { }. Ensure all brackets have been closed.
  • If all { } have been closed then move onto the next line
roblovelock
  • 1,971
  • 2
  • 23
  • 41