How do I handle line breaks in a CSV file using C#?

前端 未结 14 1885
情书的邮戳
情书的邮戳 2020-12-15 08:43

I have an Excel spreadsheet being converted into a CSV file in C#, but am having a problem dealing with line breaks. For instance:

\"John\",\"23\",\"555-555         


        
14条回答
  •  执笔经年
    2020-12-15 09:42

    A somewhat simple regular expression could be used on each line. When it matches, you process each field from the match. When it doesn't find a match, you skip that line.

    The regular expression could look something like this.

    Match match = Regex.Match(line, @"^(?:,?(?['"](?.*?\k'q')|(?[^,]*))+$");
    if (match.Success)
    {
      foreach (var capture in match.Groups["field"].Captures)
      {
        string fieldValue = capture.Value;
        // Use the value.
      }
    }
    

提交回复
热议问题