0

I have a CSV string that looks something like this:

"DOE, JOHN W", "35", "$12,945", "M"

I'm working with Java, so normally I would just split a csv with a comma as the delimiter; however, since some of the values have commas in them, I need a different way to split the string; I don't have much experience with regex at all so was wondering if anybody had any suggestions.

Thanks!

DM5963
  • 103
  • 6

2 Answers2

2

While I agree with the other comments, your best bet is to use a CSV library, there are scenarios where you may still need to parse a comma separated string with regex. For this scenario, the below regex should work.

"([^"]+)"

This regex will match any character that is not a quotation mark within quotes.

Demo

roadrunner343
  • 146
  • 1
  • 9
  • And now we wait until OP say that quotes can have escaped inner quotes. Lets simply use CSV parser which handles all kinds of traps like this one. – Pshemo Feb 28 '19 at 21:54
  • Again, I don't disagree - but I have certainly ran into scenarios where a CSV parser is not an option, primarily when scripting other platforms. In those cases, a quick regex tailored to your specific scenario can suffice. – roadrunner343 Feb 28 '19 at 21:57
1

Try this regex (maybe)

([^\",\s][^\"]*)
nissim abehcera
  • 821
  • 6
  • 7
  • first character can not be double quote or comma or space but from the second character can not be double quote only – nissim abehcera Feb 28 '19 at 21:57
  • OK, but do we really need `{1}` here? Why? `[..]` character class can match only *one* character from specified set, so `{1}` seems redundant and only provides unnecessary confusion. – Pshemo Feb 28 '19 at 22:03