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.

7
  • 1
    Did you try to use a TextFieldParser? Commented Oct 7, 2015 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 Commented Oct 7, 2015 at 22:46
  • my file is being retrieved from a FTP server, and im trying to get that info into a datatable Commented Oct 7, 2015 at 22:47
  • 1
    I have been using codeproject.com/Articles/9258/A-Fast-CSV-Reader for years. Simple, easy to use. Commented Oct 7, 2015 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). Commented Oct 7, 2015 at 23:01

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

i cant use TextFieldParser, i get an error trying to import using Microsoft.VisualBasic.FileIO; i am doing a c# console app.
This is strange because I have tested this code in a Console application. What error do you get?
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?
It would be an act of courtesy (not just for me but also for the Readers) to explain the downvote.
That has to be the most non-sensical downvote I've seen in my time on this site.....
|

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.