【问题标题】:I need a regex to split thousdands in a CSV file我需要一个正则表达式来拆分 CSV 文件中的数千个
【发布时间】:2012-08-04 15:02:08
【问题描述】:

到目前为止,我已经测试了 2 个正则表达式,但只执行了我希望它们执行的部分操作...

  • ((?:[^,"']|"[^"]*"|'[^']*')+)
  • ,(?=([^"]*"[^"]*")*[^"]*$)

这是我要拆分的数据示例...

  • 27230,419.37
  • 27232,688.95
  • 27238,409.4
  • 27240,861.92
  • 27250,176.4
  • 27254,"1,144.16"

由于它很可能是从 CSV 上传的,如果数字为 1000 或更大,那么引号内将有一个逗号。我遇到的问题是,当我执行value.split(',') 时,它会在引号之间拆分。我希望有一个正则表达式来代替一堆 for 循环和 if 语句。任何帮助将不胜感激。

(我使用的是 Apex,所以它是 ' 而不是 "

【问题讨论】:

    标签: java regex csv


    【解决方案1】:

    不要使用正则表达式,使用CSV parser

    【讨论】:

    【解决方案2】:
    String input = "27254,\"1,144.16\"";
    List<String> data = new ArrayList<String>();
    boolean inQuotes = false;
    boolean escaped = false;
    StringBuilder buf = new StringBuilder();
    for (int i = 0; i < input.length(); i++){
        char c = input.charAt(i);
        if (escaped){
            buf.append(c);
            escaped = false;
        } else if (c == '\\') {
            escaped = true;
        } else if (c == '"') {
            inQuotes = !inQuotes;
        } else if (c == ',' && !inQuotes){
            data.add(buf.toString());
            buf = new StringBuilder();
        } else {
            buf.append(c);
        }
    }
    data.add(buf.toString());
    

    【讨论】:

    • 很好的例子,但它不能正确处理转义的引号。
    • @Sjoerd 感谢 Sjoerd 的评论。我修改了代码以支持转义字符。
    • 感谢您的帖子,但我能够更简单一些。我真的很想使用正则表达式作为概念证明。
    【解决方案3】:
            for(String line : lines){    
            i++;
    
            if(skipFirst && i <= 1) continue;
            if(isBlank(line)) return error('Line ' + i + ' blank');
            pattern regex=pattern.compile(',(?=([^"]*"[^"]*")*[^"]*$)');
    
    
                cells=regex.split(line);
                string tmp0=cells.get(1).replace('"','');
    
    
            if(cells == null || cells.size() < 2) return error('Line ' + i + ' is either blank or contains only one cell');
            code = cells.get(0);
            if(code != null) code = code.trim();
            try{
                //If the amount is empty or null, assume it is 0
                if(cells.get(1) == null || cells.get(1) == ''){
                    amount = 0;
                }
                else{
                  if(!cells.get(1).contains('"')){
                        amount = Decimal.valueOf(cells.get(1));
                  }else{
                        string tmp=cells.get(1).replace('"','');
                        amount = Decimal.valueOf(tmp.replace(',',''));
                  }
    
                }
            }catch(System.TypeException e){
                return error('Line ' + i + ' contains invalid amount');
            }
            values.put(code,amount);
    
        }
    

    这篇文章是为了后代,因为我确实在salesforce内部使用正则表达式找到了一个解决方案......但是它很长并且可能没有必要。

    【讨论】:

      猜你喜欢
      • 2013-12-26
      • 1970-01-01
      • 2013-12-07
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-28
      • 2023-03-13
      • 2021-12-20
      相关资源
      最近更新 更多