基于逗号分裂忽略逗号内部逗号的正则表达式 [英] Regular Expression for Comma Based Splitting Ignoring Commas inside Quotes

查看:189
本文介绍了基于逗号分裂忽略逗号内部逗号的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个项目中,我不得不处理逗号分隔文件(CSV)。我不得不根据逗号拆分数据,忽略引号内的逗号(即)所以我使用了一个提到的表达式另一个堆栈溢出问题( Java:拆分逗号分隔的字符串,但忽略引号中的逗号)。一切都工作正常,直到最近我注意到它不适用于下面提到的一个特定场景。

In one of my projects I had to deal with Comma Separated files (CSV). I had to split data based on Comma , ignoring commas inside quotes (i.e. "") so I used an expression mentioned on another stack overflow question (Java: splitting a comma-separated string but ignoring commas in quotes). Everything was working fine until recently I noticed that it is not working for one specific scenario mentioned below.

我有一个数据字符串需要在Commas上分割为:

I have a data string needed to split on Commas as:

20Y-62-27412,20Y6227412NK,BRACKET,101H,00D505060,H664374,06/25/2013,1,,

根据我的理解基于表达式

In my understanding based on expression

String[] rowData = str.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");

分割后的数据应该返回一个大小为10的数组,其中包含最后两个数组索引包含一个空字符串,而不是我得到一个大小为8的数组,最后两个逗号不被视为拆分器。我在我的应用程序的几个地方使用了这个表达式,所以我不想从这里回溯。任何帮助将不胜感激谢谢

Data after splitting should return me an array of size 10 with last two indexes of array containing an empty string, Instead I am getting an array of size 8 being last two commas not treated as splitter. I have used this expression on several places in my application so I don't want to backtrack from this. Any help would be appreciated. Thanks

推荐答案

你需要使用 split(java.lang.String,int)
method

You need to use the split(java.lang.String, int) method

您的代码将如下所示:

String str = "20Y-62-27412,20Y6227412NK,BRACKET,101H,00D505060,H664374,06/25/2013,1,,";
String[] rowData = str.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)", -1);

这篇关于基于逗号分裂忽略逗号内部逗号的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆