【问题标题】:splitting a comma delimited string with escaping quotes用转义引号分割逗号分隔的字符串
【发布时间】:2012-10-04 23:21:28
【问题描述】:

我看到有几个类似的问题,但我没有找到任何令人满意的答案。我有一个逗号分隔的文件,其中每一行看起来像这样:

4477,52544,,,P,S,    ,,SUSAN JONES,9534 Black Bear Dr,,"CITY, NV 89506",9534 BLACK BEAR DR,,CITY,NV,89506,2008,,,,  ,     ,    , ,,1

出现的问题是,当一个标记用引号“CITY, NV 89506”转义逗号时

我需要一个结果,其中处理转义的令牌并包含每个令牌,甚至是空的。

【问题讨论】:

  • 我很想自己进行解析。好像不太难。
  • 拆分这个正则表达式,我在另一个问题上找到,让我非常接近 regex = ",(?=([^"]*"[^"]*")*[^"] *$)"。问题是我在结果中有引号。我不知道如何删除引号。
  • 这个问题其实已经被问过很多次了。关键字是相当多变的。例如,请参阅 stackoverflow.com/questions/6432408/…stackoverflow.com/questions/6428053/…
  • 您想如何处理分隔符逗号旁边的空格?例如,您的示例中的第七个值是空字符串还是 4 个空格的字符串?
  • Ted- 空格或空字符串仍应显示为值。这是挑战的一部分。

标签: java regex split tokenize


【解决方案1】:

考虑一个合适的 CSV 解析器,例如 opencsv。它将经过高度测试(与新的本土解决方案不同)并处理您所描述的边缘条件(以及许多您没有考虑过的情况)。

在下载中,有一个示例文件夹,其中包含“addresses.csv”,其中包含以下行:

Jim Sample,"3 Sample Street, Sampleville, Australia. 2615",jim@sample.com

在同一目录下,文件 AddressExample.java 解析此文件,并且与您的问题高度相关。

【讨论】:

    【解决方案2】:

    这是使用提供的java.lang.String 方法回答您的问题的一种方法。我相信它可以满足您的需求。

    private final char QUOTE = '"';
    private final char COMMA = ',';
    private final char SUB = 0x001A; // or whatever character you know will NEVER
        // appear in the input String
    
    public void readLine(String line) {
        System.out.println("original: " + line);
    
        // Replace commas inside quoted text with substitute character
        boolean quote = false;
            for (int index = 0; index < line.length(); index++) {
            char ch = line.charAt(index);
            if (ch == QUOTE) {
                quote = !quote;
            } else if (ch == COMMA && quote) {
                line = replaceChar(line, index, SUB);
                System.out.println("replaced: " + line);
            }
        }
    
        // Strip out all quotation marks
        for (int index = 0; index < line.length(); index++) {
            if (line.charAt(index) == QUOTE) {
                line = removeChar(line, index);
                System.out.println("stripped: " + line);
            }
        }
    
        // Parse input into tokens
        String[] tokens = line.split(",");
        // restore commas in place of SUB characters
        for (int i = 0; i < tokens.length; i++) {
            tokens[i] = tokens[i].replace(SUB, COMMA);
        }
    
        // Display final results
        System.out.println("Final Parsed Tokens: ");
        for (String token : tokens) {
            System.out.println("[" + token + "]");
        }
    }
    
    private String replaceChar(String input, int position, char replacement) {
        String begin = input.substring(0, position);
        String end = input.substring(position + 1, input.length());
        return begin + replacement + end;
    }
    
    private String removeChar(String input, int position) {
        String begin = input.substring(0, position);
        String end = input.substring(position + 1, input.length());
        return begin + end;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-30
      • 1970-01-01
      • 2017-01-05
      • 2016-03-21
      • 1970-01-01
      相关资源
      最近更新 更多