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 ?

3
  • Probably (?:^|,)(\"(?:[^\"]+|\"\")*\"|[^,]*) or better a CSV parser ! Commented Aug 2, 2013 at 12:39
  • 1
    You should use a CSV parser library. Commented Aug 2, 2013 at 12:39
  • stackoverflow.com/questions/101100/csv-api-for-java Commented Aug 2, 2013 at 12:40

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

Unless you are good at regex :)
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

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.