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

新手上路,请多包涵

我有一个像这样的字符串:

 foo,bar,c;qual="baz,blurb",d;junk="quux,syzygy"

我想用逗号分隔——但我需要忽略引号中的逗号。我怎样才能做到这一点?似乎正则表达式方法失败了;我想我可以在看到引用时手动扫描并进入不同的模式,但是使用预先存在的库会很好。 ( _编辑_:我想我的意思是库已经是 JDK 的一部分,或者已经是常用库(如 Apache Commons)的一部分。)

上面的字符串应该分成:

 foo
bar
c;qual="baz,blurb"
d;junk="quux,syzygy"

注意: 这不是 CSV 文件,它是包含在具有更大整体结构的文件中的单个字符串

原文由 Jason S 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.3k
2 个回答

尝试:

 public class Main {
    public static void main(String[] args) {
        String line = "foo,bar,c;qual=\"baz,blurb\",d;junk=\"quux,syzygy\"";
        String[] tokens = line.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1);
        for(String t : tokens) {
            System.out.println("> "+t);
        }
    }
}

输出:

 > foo
> bar
> c;qual="baz,blurb"
> d;junk="quux,syzygy"

换句话说: _仅当逗号前面有零或偶数个引号时才按逗号拆分_。

或者,对眼睛更友好一点:

 public class Main {
    public static void main(String[] args) {
        String line = "foo,bar,c;qual=\"baz,blurb\",d;junk=\"quux,syzygy\"";

        String otherThanQuote = " [^\"] ";
        String quotedString = String.format(" \" %s* \" ", otherThanQuote);
        String regex = String.format("(?x) "+ // enable comments, ignore white spaces
                ",                         "+ // match a comma
                "(?=                       "+ // start positive look ahead
                "  (?:                     "+ //   start non-capturing group 1
                "    %s*                   "+ //     match 'otherThanQuote' zero or more times
                "    %s                    "+ //     match 'quotedString'
                "  )*                      "+ //   end group 1 and repeat it zero or more times
                "  %s*                     "+ //   match 'otherThanQuote'
                "  $                       "+ // match the end of the string
                ")                         ", // stop positive look ahead
                otherThanQuote, quotedString, otherThanQuote);

        String[] tokens = line.split(regex, -1);
        for(String t : tokens) {
            System.out.println("> "+t);
        }
    }
}

产生与第一个示例相同的结果。

编辑

正如@MikeFHay 在评论中提到的:

我更喜欢使用 Guava 的 Splitter ,因为它具有更合理的默认值(请参阅上面关于被 String#split() 修剪的空匹配的讨论,所以我这样做了:

 Splitter.on(Pattern.compile(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"))

原文由 Bart Kiers 发布,翻译遵循 CC BY-SA 4.0 许可协议

虽然我确实喜欢一般的正则表达式,但对于这种依赖于状态的标记化,我相信一个简单的解析器(在这种情况下比这个词听起来要简单得多)可能是一个更清晰的解决方案,特别是在可维护性方面,例如:

 String input = "foo,bar,c;qual=\"baz,blurb\",d;junk=\"quux,syzygy\"";
List<String> result = new ArrayList<String>();
int start = 0;
boolean inQuotes = false;
for (int current = 0; current < input.length(); current++) {
    if (input.charAt(current) == '\"') inQuotes = !inQuotes; // toggle state
    else if (input.charAt(current) == ',' && !inQuotes) {
        result.add(input.substring(start, current));
        start = current + 1;
    }
}
result.add(input.substring(start));

如果您不关心在引号内保留逗号,您可以通过将引号中的逗号替换为其他内容然后在逗号处拆分来简化这种方法(不处理起始索引,没有 最后一个字符 的特殊情况):

 String input = "foo,bar,c;qual=\"baz,blurb\",d;junk=\"quux,syzygy\"";
StringBuilder builder = new StringBuilder(input);
boolean inQuotes = false;
for (int currentIndex = 0; currentIndex < builder.length(); currentIndex++) {
    char currentChar = builder.charAt(currentIndex);
    if (currentChar == '\"') inQuotes = !inQuotes; // toggle state
    if (currentChar == ',' && inQuotes) {
        builder.setCharAt(currentIndex, ';'); // or '♡', and replace later
    }
}
List<String> result = Arrays.asList(builder.toString().split(","));

原文由 Fabian Steeg 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题