简体   繁体   中英

Java Regex - Split comma separated list but exclude commas within square brackets

I am trying to split the string below, but I want to exclude everything in the [[......]] . I have tried using multiple suggestions that I have found here already, but none of them are working.

[675: test, 676: test1, 677: test2, 678: [[{"id":0,"value":"15"},{"id":1,"value":"2"},
{"id":2,"value":"2"}],[{"id":0,"value":"2"},{"id":1,"value":"3"},
{"id":2,"value":"3"}],[{"id":0,"value":"5"},{"id":1,"value":"6"},
{"id":2,"value":"6"}],[{"id":0,"value":"7"},{"id":1,"value":"8"}],
[{"id":0,"value":"99"},{"id":1,"value":"8"},{"id":2,"value":"7"}]]]

I want to be able to get a result like this:

675: test 
676: test1 
677: test2 
678: [[{"id":0,"value":"15"},{"id":1,"value":"2"}, {"id":2,"value":"2"}], 
     [{"id":0,"value":"2"},{"id":1,"value":"3"},{"id":2,"value":"3"}],       
     [{"id":0,"value":"5"},{"id":1,"value":"6"}, {"id":2,"value":"6"}],
     [{"id":0,"value":"7"},{"id":1,"value":"8"}],
     [{"id":0,"value":"99"},{"id":1,"value":"8"},{"id":2,"value":"7"}]]]

I tried splitting the string by commas but that split up all the data contained within the square brackets also.

Thanks

Except for the outer set of [] , that looks like JSON, so you could replace the outer [] s with {} s and parse it as JSON.

Edit: I was a bit too quick to reply. JSON keys need to be strings, and test , test1 etc. would also have had to be in quotes for this to be JSON. The following code splits on the top level commas and prints out the trimmed strings around them, dropping the outer [] s.

....
  public static void parse(String s) {
    boolean quote = false;
    int depth = 0;
    int splitPoint = 1; // drop the first '['
    for (int i = 0; i < s.length(); i++) {
      char c = s.charAt(i);
      switch (c) {
        case '"':
          quote = !quote;
          break;
        case '{':
        case '[':
          if (!quote) {
            depth += 1;
          }
          break;
        case '}':
        case ']':
          if (!quote) {
            depth -= 1;
          }
          break;
        case ',':
          if (!quote && depth == 1) {
            System.out.println(s.substring(splitPoint, i).trim()); // or store
            splitPoint = i + 1;
          }
          break;
      }
    }
    System.out.println(s.substring(splitPoint, s.length() - 1).trim()); // or store
  }
....

Output:

675: test
676: test1
677: test2
678: [[{"id":0,"value":"15"},{"id":1,"value":"2"},{"id":2,"value":"2"}],[{"id":0,"value":"2"},{"id":1,"value":"3"},{"id":2,"value":"3"}],[{"id":0,"value":"5"},{"id":1,"value":"6"},{"id":2,"value":"6"}],[{"id":0,"value":"7"},{"id":1,"value":"8"}],  [{"id":0,"value":"99"},
{"id":1,"value":"8"},{"id":2,"value":"7"}]]

If you have access to the code that builds that string, you would ideally have it print something that's directly parseable as JSON, and then you would have no need to do manual processing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM