只有在双引号c#之外的昏迷时才用逗号分割字符串

时间:2014-08-19 12:58:17

标签: c# asp.net regex

"TIMESTAMP (UTC)","LOG TYPE","DEVICE TYPE","DEVICE","MESSAGE","PARAMETERS"
"2014-08-12 17:30:34.437","Warning","DiverGate","141403G00294","Diver gate(s) did not connect since","2014-08-08 06:37:31 (UTC)"
"2014-08-12 17:30:34.577","Warning","DiverGate","141403G00120","Diver gate(s) did not connect since","2014-08-08 06:46:22 (UTC)"
"2014-08-13 06:45:18.890","Error","DiverGate","141403G00294","Was set to inactive, because it did not connect since","2014-08-08 06:37:31 (UTC)"
"2014-08-13 07:00:18.903","Error","DiverGate","141403G00120","Was set to inactive, because it did not connect since","2014-08-08 06:46:22 (UTC)"

这是我的.csv文件,我需要从文件中读取信息,但我需要用逗号分隔双引号之外的信息,因为在其他一些文件中我可以找到逗号到某些信息,特别是在消息中,日志类型,...

 string url = @"E:\Project.csv";
 Stream stream = File.Open(url, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            string[] lines = null;

            using (StreamReader sr = new StreamReader(stream))
            {
                string str = sr.ReadToEnd();
                lines = Regex.Split(str, //what expression is going here);
            }

4 个答案:

答案 0 :(得分:3)

这只是基本的CSV解析,并且已经有了库来完成它。我建议你看一下我之前用过的CsvHelper,而不是试图重新发明轮子。

您可以使用软件包管理器控制台并输入以下内容,轻松地将其包含在项目中:

  

Install-Package CsvHelper

答案 1 :(得分:2)

您可以尝试使用Lookaround

  

字符串中没有consume个字符,但只断言match是否可行。

(?<="),(?=")

以下是online demo并在regexstorm

进行了测试

模式解释非常简单

  (?<=                     look behind to see if there is:
    "                        '"'
  )                        end of look-behind
  ,                        ','
  (?=                      look ahead to see if there is:
    "                        '"'
  )                        end of look-ahead

答案 2 :(得分:1)

使用现有库,而不是推出自己的CSV解析器。 Visual Basic提供了TextFieldParser类,只需在项目引用下添加对Microsoft.VisualBasic的引用即可:

TextFieldParser textFieldParser = new TextFieldParser(@"E:\Project.csv");
textFieldParser.TextFieldType = FieldType.Delimited;
textFieldParser.SetDelimiters(",");
while (!textFieldParser.EndOfData)
{
    string[] values = textFieldParser.ReadFields();
    Console.WriteLine(string.Join("---", values));//printing the row
}
textFieldParser.Close();

答案 3 :(得分:1)

嘿,你也可以使用这个正则表达式

var result = Regex.Split(samplestring, ",(?=(?:[^']*'[^']*')*[^']*$)");