2015-11-13 78 views
-1

我在Regex中并不是很出色。正则表达式分割为逗号和双引号CSV格式

字符串:

"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","","" 

我已经这样做了,但之前删除双引号。但字符串Report,LicenseI-Web Report,License的结果被分割。这是错误的。

我想通过双引号之间的逗号分割它,而不是在它们内部。

+0

我会做手动。你知道,'List ','''loop和'Substring'。我猜,这是更高性能的。 –

+0

var values = Regex.Split(line,“,(?=(?:[^ \”] * \“[^ \”] * \“)* [^ \”] * $)“); – evilom

回答

1

使用真正的csv解析器而不是使用字符串方法或正则表达式。您可以使用TextFieldParser这是直接的架构中唯一一个可供选择:

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); 
    } 
} 

您需要到Microsoft.VisualBasic DLL的引用添加到项目中。

还有其他可用:Parsing CSV files in C#, with header