0

I've been trying my luck with Regex but my understanding doesn't seem to be the best.

Problem I have a .csv file given to me by a 3rd party. I cannot edit it but need to read the data into my application.

There are always 12 columns in the file. However, sometimes it will go like this:

text, text ,text,"text with comma,"

text, text, text, text....

text, text, text,"text with comma,","text with comma again", text...

What I need to do this replace all the commas between the "" with a -.

Any help would be appreciated!

Community
  • 1
  • 1
Lift
  • 546
  • 2
  • 4
  • 24
  • 5
    [Are there any CSV readers/writer libraries in C#?](http://stackoverflow.com/q/1941392/669576) – 001 Dec 28 '16 at 02:08
  • Johnny - Did not see that post in my travels. I'll look into it, seems like it's what I need. Thanks. – Lift Dec 28 '16 at 02:09

1 Answers1

4

This might do the trick for you

foreach(Match match in Regex.Matches(YourCSV, "\"([^\"]*)\""))
    if(match.ToString().Contains(","))
        YourCSV = YourCSV.Replace(match.ToString(), match.ToString().Replace(",", "-"));
Mohit S
  • 13,723
  • 6
  • 34
  • 69