Read csv file with java - comma delimiter in text field?

Unless this is homework you should not parse CSV yourself. Use one of existing libraries. For example this one: commons.apache.org/sandbox/csv.

Unless this is homework you should not parse CSV yourself. Use one of existing libraries. For example this one: commons.apache.org/sandbox/csv/ Or google "java csv parser" and choose another.

But if you wish to implement the logic yourself you should use negative lookahead feature of regular expressions (see download.oracle.com/javase/1,5.0/docs/ap...).

Thanks Alex , I found some libraries for parsing CSV files . Finally I used opencsv – Toren Nov 9 at 21:18.

Your safest bet is you use csv parsing library. Your comma is enclosed in quotes. You'd need to implement logic to look for quoted commas.

However you'd also need to plan for other situations, like quote within a quote, escape sequences etc. Better use some ready for use and tested solution. Use google, you'll find some. CSV files can be tricky to use on your own.

I hope you can remove \ \ s * from your regular expression. Then have: while (s.hasNext() { String symbol = s.next(); if (symbol. StartsWith("\"")) { while ((symbol.

EndsWith("\"") || symbol.length() == 1) && s.hasNext()) { symbol += "," + s.next(); } } ...

Thank you Joop for nice sharing – Toren Nov 9 at 21:16.

As others have correctly pointed out, rolling your own csv parser is not a good idea as it will usually leave huge security holes in your system. That said, I use this regex: "((?:\"^\"*? \")*|^\"^,$*?)(,$)" which does a good job with well-formed csv data.

You will need to use a Pattern and a Matcher with it. This is what it does: /* ( - Field Group (?: - Non-greedy consume of quoted strings \" - Start with a quote ^\"*? - Non-greedy match on anything that is not a quote \" - End with a quote )* - And repeat | - Or ^\" - Not starting with a quote ^,$*?

- Non-greedy match on anything that is not a comma or end-of-line ) - End field group ( - Separator group ,$ - Comma separator or end of line ) - End separator group */ Note that it parses the data into two groups, the field and the separator. It also leaves the quote characters in the field, you may wish to remove them and replace "" with " etc.

Paul , thank you for sharing the regex . Since I saw your answer I started to work on my own regex. – Toren Nov 9 at 21:14.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions