0

i am trying to convert a string like

"test",645,23.4,42,"13,13,14","test"

into

"test","645","23.4","42","13,13,14","test"

i am trying with this code.

string pattern = "\",(? !\")";
string pattern2 = "(?<!\"),(? !\")";
string pattern3 = "(?<!\"),\"";
string replacement = "\",\"";

Regex rgx = new Regex(pattern);
catalogo = rgx.Replace(catalogo, replacement);
rgx = new Regex(pattern2);
catalogo = rgx.Replace(catalogo, replacement);
rgx = new Regex(pattern3);
catalogo = rgx.Replace(catalogo, replacement);

but i dont know how to get past the value that already contains commas. "13,13,14" since it will change it into "13","13","14"

i dont know if thats the best way to convert the string, but at least i believe that it will do the job, just that i dont know why i get past this.

pato.llaguno
  • 741
  • 4
  • 20
  • 1
    Did you try to use a TextFieldParser? – Steve Oct 07 '15 at 22:43
  • @Steve i dont know what that is, ill look for it, but if you are kind to give me your thoughts on how i could use that i would be gratefull – pato.llaguno Oct 07 '15 at 22:46
  • my file is being retrieved from a FTP server, and im trying to get that info into a datatable – pato.llaguno Oct 07 '15 at 22:47
  • 1
    I have been using http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader for years. Simple, easy to use. – Eric J. Oct 07 '15 at 22:48
  • I definitely recommend `TextFieldParser` - it's part of the .NET framework and it works well. I've used it several times (all from C# apps). – Tim Oct 07 '15 at 23:01
  • Possible duplicate of [CSV parser/reader for C#?](http://stackoverflow.com/questions/906841/csv-parser-reader-for-c) – ivan_pozdeev Oct 08 '15 at 11:40
  • @ivan_pozdeev so my question was actually of a regex.. these guys gave me another solution, that doesn't make this a re-post of the solution they suggested. – pato.llaguno Oct 08 '15 at 15:41

1 Answers1

4

I am not sure why you need to use a Regex to parse comma separated data.
There are a lot of free libraries specialized in parsing this kind of data and the same NET Framework provides a specific class in the Microsoft.VisualBasic.IO namespace

Here how you could use it

string t = "\"test\",645,23.4,42,\"13,13,14\",\"test\"";
StringReader sr = new StringReader(t);
TextFieldParser tp = new TextFieldParser(sr);
tp.Delimiters = new string[] {","};
tp.HasFieldsEnclosedInQuotes = true;

string[] result = tp.ReadFields();

foreach(string s in result)
   Console.WriteLine(s);

This code retrieves your data and respect the fields enclosed in double quotes avoiding to parse the content of these fields. However it seems that you want also a double quote arounde each string retrieved so you need a second loop to readd the missing quotes

for(int x = 0; x < result.Length; x++)
    result[x] = string.Concat("\"", result[x], "\"");

By the way, I am not recommending to use this class, free libraries or custom code. To choice between these options is always a tradeoff between costs (write, debug, test, document) and perfomances. If performance is a critical aspect of your solution then you need to test a lot of things using the real data coming from your FTP server. A task that only you could perform.

Steve
  • 213,761
  • 22
  • 232
  • 286
  • i cant use TextFieldParser, i get an error trying to import using Microsoft.VisualBasic.FileIO; i am doing a c# console app. – pato.llaguno Oct 07 '15 at 22:53
  • This is strange because I have tested this code in a Console application. What error do you get? – Steve Oct 07 '15 at 22:57
  • I've used `TextFieldParser` in several C# applications - there shouldn't be any issue with it. Did you add a reference to `Microsoft.VisualBasic.FileIO` in your project? – Tim Oct 07 '15 at 23:00
  • It would be an act of courtesy (not just for me but also for the Readers) to explain the downvote. – Steve Oct 07 '15 at 23:03
  • That has to be the most non-sensical downvote I've seen in my time on this site..... – Tim Oct 07 '15 at 23:05
  • it wont let me import the FileIO i get `the type or namespace FileIO does not exist in the namespace Microsoft.VisualBasic` – pato.llaguno Oct 08 '15 at 01:17
  • added a reference and now i can use it, your example does work, ill try it with my actual code. THANKS! – pato.llaguno Oct 08 '15 at 01:28
  • @Steve since this will give me an array with all the values how would i read the csv file? i would do this for every line? or is there another way? I need to get this data to a datatable – pato.llaguno Oct 08 '15 at 01:41