-1

I am not really good in Regex.

The string:

"FF","asdadasd60","Report,License","502","5A1301","I-Web Report,License","50A1","PR02","02","5A11","REL","","","","A1170600","500008","FA10","5000001","","","","","000000000.000","","000000000.000","","000000000.000","","000000000.000","","00000000","00000000","",""

I have done this but remove the double quotes before. But the result for string Report,License and I-Web Report,License are splitted. This is wrong.

I want to split it into array by comma between double quotes not inside them.

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
andrefadila
  • 647
  • 2
  • 9
  • 36

1 Answers1

2

Use a real csv parser instead of using string methods or regex. You could use the TextFieldParser which is the only one available in the framework directly:

var allLineFields = new List<string[]>();
using (var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(new StringReader(str)))
{
    parser.Delimiters = new string[] { "," };
    parser.HasFieldsEnclosedInQuotes = true; // <--- !!!
    string[] lineFields;
    while ((lineFields = parser.ReadFields()) != null)
    {
        allLineFields.Add(lineFields);
    }
}

You need to add a reference to the Microsoft.VisualBasic dll to your project.

There are other available: Parsing CSV files in C#, with header

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939