1

I need to write a Java program that transform a CSV file into an array list. A CSV line looks like this:

5/3/2010,"Behrens, Michael Lakeside Apts","Lisle, IL 60532",PD: Noise or domestic reports for 3/25 to 4/28 for specific apt.,5/10/2010, 5/10/2010,1

I need it like this:

DATE RECEIVED:5/3/2010  
NAME / COMPANY:Behrens, Michael Lakeside Apts  
CITY, STATE, ZIP:Lisle, IL 60532  
Department / Documents:PD: Noise or domestic reports for 3/25 to 4/28 for specific apt.   
Due Date 5 Days 10 Days:5/10/2010  
DATE COMPLETED:5/10/2010  
Hours Spent Fulfilling Request:1

So far I've got this: ,|[\"] but I need to remove , from ""

1 Answer 1

4

What you need isn't regex, it's a csv parser (openCSV).

Alternatively you could do this manually using something like

String[] arr = string.split("(\\\",\\\"|(?<!\\\"),)");
String output = "DATE RECEIVED: " + arr[0] + 
                "\nZIP: " + ...
                "...  " + ...;
Sign up to request clarification or add additional context in comments.

7 Comments

he has comma inside a quote so simple spliting does not work well. i doubt any csv parser could do this.
You're right, I didn't pay enough attention, changed it to some magic regex :)
The parentheses in your regex are mismatched - there's one more ) than (.
The parser suggested by Johan (openCSV) can parse this correctly. It will work fine as long as commas only appear in quoted values.
Then the csv parser should be used.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.