简体   繁体   English

java 中任意字符串或字符数的正则表达式构造

[英]Regular expression construct for arbitrary string or number of characters in java

I am trying to split data from a.csv file, however, some of the fields/columns also contain commas in between just like this我正在尝试从 a.csv 文件中拆分数据,但是,某些字段/列之间也包含逗号,就像这样

ABCKS,"ASK,ED","SDR,ED",2022-07-11,8011.0 ABCKS,"ASK,ED","SDR,ED",2022-07-11,8011.0

cvbgb,"hfhvnf,rgr","dthd,chdf",2022-07-11,111.9 cvbgb,"hfhvnf,rgr","dthd,chdf",2022-07-11,111.9

ABCKS,"ASK,ED","SDR,ED",2022-07-11,8011.0 ABCKS,"ASK,ED","SDR,ED",2022-07-11,8011.0

hence, the.split(",") string method would create additional fields into the data.因此,.split(",") 字符串方法会在数据中创建额外的字段。

I have tried我努力了

    if (aLine.contains("\"|,|\"")){

        String newString = aLine.replaceAll("\"|,|\"","|_|").replaceAll("\"", "");
        aList = Arrays.asList(newString.split(",", -1));

    }

It does not seem to work.它似乎不起作用。 Please, any help would be appreciated.请,任何帮助将不胜感激。

As Tim said above, a proper CSV reader would probably be better but a simple solution could be something like this: ,(?![A-Za-z]+") . Where you select every comma that is not followed by letters and a quotation. This satisfies your sample data but if there are edge cases it can easily break.正如蒂姆上面所说,一个合适的 CSV 阅读器可能会更好,但一个简单的解决方案可能是这样的: ,(?![A-Za-z]+") 。你在 select 每个逗号后面不跟字母和报价。这满足您的示例数据,但如果有边缘情况,它很容易破坏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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