简体   繁体   English

使用String.split()拆分时转义','分隔符

[英]Escaping ',' separator while splitting using String.split()

I am trying to read a file and hence I am splitting the fields when I receive ',' comma separator . 我正在尝试读取文件,因此当我收到','逗号分隔符时,我将分割字段。 However some fields have ',' in them but they are enclosed within double quotes hence how can I split it escaping the , separator. 然而,某些领域有','在他们,但他们是双引号,因此我怎么能拆呢逃脱内封闭,分离器。 Here is what I have done 这就是我所做的

String[] cols = line.split(Pattern.quote(","));

How should I modify this using split() only in Java. 我应该如何使用split()仅在Java中修改它。 Also what changes will I have to make in case the separator is a pipe '|' 如果分隔符是管道'|'我还需要进行哪些更改 ?

I answered a similar question here . 在这里回答了类似的问题。 The first expression, modified for your task, would read 为您的任务修改的第一个表达式将会读取

,(?=([^"]*"[^"]*")*[^"]*$)

This expression identifies an unquoted comma by ensuring that an even number of quotation marks follows it. 此表达式通过确保在其后面引用偶数引号来标识不带引号的逗号。

I wouldn't try using a regex for this. 我不会尝试使用正则表达式。 Regular expressions are just not a great match for this - while it may be possible to create such a regex, it would be horrible to read. 正则表达式并不是一个很好的匹配 - 虽然有可能创建这样的正则表达式,但阅读起来会很糟糕。

There are plenty of open source CSV parsers. 有很多开源CSV解析器。 Just a quick search found many projects - I would look through those before writing your own. 只需快速搜索可以找到很多项目 - 在编写自己的项目之前我会仔细查看。

    String  line="one|two,three";
    String cols[]= line.split("[,|\\|]");

Something like the above would split based on , and | 像上面这样的东西会基于和分裂

For meta-character | 对于元字符| you would have to delimit with \\ \\ I agree with others; 你必须划定\\ \\我同意他人; it's better to use CSV parsers out there rather than reinventing it again. 最好在那里使用CSV解析器,而不是再次重新发明它。

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

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