Regular expression for comma-based separation Ignoring commas inside quotes

In one of my projects, I had to deal with comma separated files (CSV). I had to break Comma-based data, ignoring the commas inside quotation marks (i.e. "" ), so I used the expression mentioned in another question ( Java: comma-separated string, but ignoring commas in quotation marks ). Everything worked fine until recently, I noticed that it does not work for one specific scenario, mentioned below.

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

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

In my understanding, based on the expression

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

The data after separation should return to me an array of size 10 with the last two indexes of the array containing an empty string. Instead, I get an array of size 8, which is the last two commas that are not considered as a splitter. I used this expression in several places in my application, so I do not want to backtrack on this. Any help would be greatly appreciated. Thanks

+1
java string regex
source share
1 answer

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

Then your code will look like this:

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

All Articles