I have csv that comes with format:
a1, a2, a3, "a4,a5", a6
Only field with , will have quotes
Using Java, how to easily parse this? I try to avoid using open source CSV parser as company policy. Thanks.
Yes. You can import double quotation marks using CSV files and import maps by escaping the double quotation marks. To escape the double quotation marks, enclose them within another double quotation mark.
ISSUE: A CSV file contains data that consists of a bunch of fields separated by a comma and optionally enclosed by double-quotes, hence the name Comma-Separated-Values or CSV. Due to the lack of an actual standard for CSV formatting, some programs may opt to use semi-colons instead of commas as separators.
For read. csv(), the default quote parameter is quote="\"", which means that only double quotes will be used to delimit strings, not single quotes. Because two of your sample names had apostrophes (single quotes), the read. table() function tried to include everything between those two as a single string.
You could use Matcher.find
with the following regular expression:
\s*("[^"]*"|[^,]*)\s*
Here's a more complete example:
String s = "a1, a2, a3, \"a4,a5\", a6"; Pattern pattern = Pattern.compile("\\s*(\"[^\"]*\"|[^,]*)\\s*"); Matcher matcher = pattern.matcher(s); while (matcher.find()) { System.out.println(matcher.group(1)); }
See it working online: ideone
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