0

i have the following csv file:

"Maria,"Emilia",Brown",Martinez,5,40

I want the following outcome:

  • "Maria,"Emilia",Brown"
  • Martinez
  • 5
  • 40

I use the following matcher:

Matcher m = Pattern.compile("\"([^\"]+?)\"|(?<=,|^)([^,]*)(?=,|$)").matcher(line);

But instead i get the following result:

  • "Maria,"
  • ",Brown"
  • Martinez
  • 5

How the hell can i fix it. I tried everything, but i can't seem to find a good pattern.

  • May be a dupe?: http://stackoverflow.com/questions/1757065/java-splitting-a-comma-separated-string-but-ignoring-commas-in-quotes – Steve Smith Apr 26 '17 at 14:59
  • Nah, my method don't have the same problem –  Apr 26 '17 at 15:00
  • The answer http://stackoverflow.com/a/2120714/1551685 should get you started though. – Steve Smith Apr 26 '17 at 15:03
  • Welcome to the world of CSV processing. There is no absolute answer because CSV formats vary. Also, regexes are notoriously finicky beasts and perhaps not always the best approach. – Lew Bloch Apr 26 '17 at 16:21

1 Answers1

0

This regex should work :

(".*"),(.*),(.*),(.*)

Don't forget to escape double quotes in your java String.

S. Blanc
  • 11
  • 2
  • Yeah, that won't work. The first capture group will match `"Maria,"`, and the overall expression doesn't match the input shown. So that's wrong. And you don't necessarily get embedded quotes, or the same number of them, or just in the first item. So that's wrong, too. – Lew Bloch Apr 26 '17 at 16:24