简体   繁体   中英

Regular Expression for Comma Based Splitting Ignoring Commas inside Quotes

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 (ie "" ) 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.

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(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");

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

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

Your code would then look like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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