正则表达式(C#)对于RFC通过RFC 4180 [英] Regular Expression (C#) For CSV by RFC 4180
问题描述
需要通过规范RFC 4180 提供的通用CSV解析器。
有一个csv文件,其中包含规范的所有问题:
Requires universal CSV parser by specification RFC 4180. There is the csv file, with all the problems of the specification:
Excel打开文件时,它是写在规范:
Excel opens the file as it is written in the specification:
任何人都可以正常工作正则表达式来解析它?
Anyone does work regex for parse it?
CSV文件
aapers
bogle
c,x
$ $ $ $ bz,357
test; test,xxx; xxx,152
test2,test2,xxx2,xxx2,123
test3test3 ,xxx3xxx3,987
,qwe,13
asd,123,
$ $ $ b ,,,
,123,
,, 123
$
123 ,,,
123,123
"a
b
c","x
y
z",357
test;test,xxx;xxx,152
"test2,test2","xxx2,xxx2",123
"test3""test3","xxx3""xxx3",987
,qwe,13
asd,123,
,,
,123,
,,123
123,,
123,123
预期结果
推荐答案
我会说,忘了regex。 CSV可以通过TextFieldParser类轻松解析。为此,您需要使用Microsoft.VisualBasic.FileIO来
I would say, forget about regex. CSV can be parsed easily by TextFieldParser class. To do that, you need to be
using Microsoft.VisualBasic.FileIO;
然后您可以使用它:
using (TextFieldParser parser = new TextFieldParser(Stream))
{
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters(",");
while (!parser.EndOfData)
{
string[] fields = parser.ReadFields();
foreach (string field in fields)
{
// Do your stuff here ...
}
}
}
这篇关于正则表达式(C#)对于RFC通过RFC 4180的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!