0

I'm trying to seperate a csv file values using a regex expression, The problem is that there are quotation marks surrounding sentences and the regex I've tried doesn't seem to do it correctly.

"All's Well, Ends Well 2012",Comedy

This is one of the strings that is contained in the CSV and I've tried separating it with quite a few different regex statements but it keeps just returning the whole string without seperating it.

Any advice or work arounds regarding the regex I can use for this ?

Crossman
  • 278
  • 5
  • 18

2 Answers2

4

The best advice is to use a CVS parser, eg http://opencsv.sourceforge.net/, regex is not the best choice for parsing CSV

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
1

Use a regex with split() that splits by commas only when an even number of quotes appear in the rest of the input:

String[] parts = line.split(",(?=(([^\"]*\"){2})*[^\"]*$)");

See this code execute in a live demo on ideone

animuson
  • 53,861
  • 28
  • 137
  • 147
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Thank you this works perfectly ^^ I'm using it on processing and not sure if I can include external libraries in it. so thanks a lot – Crossman Aug 02 '13 at 12:45