I have to split a string using comma(,) as a separator and ignore any comma that is inside quotes(")
fieldSeparator : ,
fieldGrouper : "
The string to split is : "1","2",3,"4,5"
I am able to achieve it as follows :
String record = "\"1\",\"2\",3,\"4,5\"";
String[] tokens = record.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
Output :
"1"
"2"
3
"4,5"
Now the challenge is that the fieldGrouper(") should not be a part of the split tokens. I am unable to figure out the regex for this.
The expected output of the split is :
1
2
3
4,5
Update:
String[] tokens = record.split( "(,*\",*\"*)" );
Result:
Initial Solution:
( doesn't work @.split
method )This RexEx pattern will isolate the sections you want:
(?:\\")(.*?)(?:\\")
It uses non-capturing groups to isolate the pairs of escaped quotes, and a capturing group to isolate everything in between.
Check it out here: Live Demo
My suggestion:
"([^"]+)"|(?<=,|^)([^,]*)
See the regex demo. It will match "..."
like strings and capture into Group 1 only what is in-between the quotes, and then will match and capture into Group 2 sequences of characters other than ,
at the start of a string or after a comma.
Here is a Java sample code:
String s = "value1,\"1\",\"2\",3,\"4,5\",value2";
Pattern pattern = Pattern.compile("\"([^\"]+)\"|(?<=,|^)([^,]*)");
Matcher matcher = pattern.matcher(s);
List<String> res = new ArrayList<String>();
while (matcher.find()){ // Run the matcher
if (matcher.group(1) != null) { // If Group 1 matched
res.add(matcher.group(1)); // Add it to the resulting array
} else {
res.add(matcher.group(2)); // Add Group 2 as it got matched
}
}
System.out.println(res); // => [value1, 1, 2, 3, 4,5, value2]
I would try with this kind of workaround:
String record = "\"1\",\"2\",3,\"4,5\"";
record = record.replaceAll("\"?(?<!\"\\w{1,9999}),\"?|\""," ");
String[] tokens = record.trim().split(" ");
for(String str : tokens){
System.out.println(str);
}
Output:
1
2
3
4,5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With