Java Regex - 拆分逗号分隔列表但排除方括号内的逗号

Posted

技术标签:

【中文标题】Java Regex - 拆分逗号分隔列表但排除方括号内的逗号【英文标题】:Java Regex - Split comma separated list but exclude commas within square brackets 【发布时间】:2015-09-10 09:23:32 【问题描述】:

我正在尝试拆分下面的字符串,但我想排除 [[......]] 中的所有内容。我已经尝试使用我已经在这里找到的多个建议,但它们都不起作用。

[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"]]]

我希望能够得到这样的结果:

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"]]]

我尝试用逗号分割字符串,但这也分割了方括号中包含的所有数据。

谢谢

【问题讨论】:

您可以先在“[[”上拆分,然后在“,”上拆分结果[0] 【参考方案1】:

除了[] 的外部集合之外,它看起来像 JSON,因此您可以将外部 []s 替换为 s 并将其解析为 JSON。

编辑:我回复得太快了。 JSON 键必须是字符串,testtest1 等也必须用引号括起来才能成为 JSON。以下代码在***逗号上拆分并打印出围绕它们的修剪字符串,删除外部[]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
  
....

输出:

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"]]

如果您有权访问构建该字符串的代码,理想情况下,您可以让它打印可直接解析为 JSON 的内容,然后您就无需进行手动处理。

【讨论】:

以上是关于Java Regex - 拆分逗号分隔列表但排除方括号内的逗号的主要内容,如果未能解决你的问题,请参考以下文章

Java:拆分逗号分隔的字符串但忽略引号中的逗号

PHP 和 RegEx:用不在括号内的逗号(以及嵌套括号)拆分字符串

MySQL REGEX匹配逗号分隔列表

拆分由逗号分隔的 JSON blob 列表(忽略 JSON blob 中的逗号)[重复]

如何将逗号分隔的字符串拆分为字符串列表?

在逗号上拆分字符串但忽略双引号内的逗号?