regex to parse csv

前端 未结 3 502
独厮守ぢ
独厮守ぢ 2021-01-19 18:51

I\'m looking for a regex that will parse a line at a time from a csv file. basically, what string.readline() does, but it will allow line breaks if they are within double q

相关标签:
3条回答
  • 2021-01-19 19:26

    The FileHelpers library is pretty good for this purpose.

    http://www.filehelpers.net/

    0 讨论(0)
  • 2021-01-19 19:33

    Using regex to parse CSV is fine for simple applications in well-controlled CSV data, but there are often so many gotchas, such as escaping for embedded quotes and commas in quoted strings, etc. This often makes regex tricky and risky for this task.

    I recommend a well-tested CSV module for your purpose.

    --Edit:-- See this excellent article, Stop Rolling Your Own CSV Parser!

    0 讨论(0)
  • 2021-01-19 19:35

    Rather than relying on error prone regular expressions, over simpified "split" logic or 3rd party components, use the .NET framework's built in functionality:

    Using Reader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\MyFile.csv")
    
        Reader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
    
        Dim MyDelimeters(0 To 0) As String
        Reader.HasFieldsEnclosedInQuotes = False
        Reader.SetDelimiters(","c)
    
        Dim currentRow As String()
        While Not Reader.EndOfData
            Try
                currentRow = Reader.ReadFields()
                Dim currentField As String
                For Each currentField In currentRow
                    MsgBox(currentField)
                Next
            Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                MsgBox("Line " & ex.Message &
                "is not valid and will be skipped.")
            End Try
        End While
    End Using
    
    0 讨论(0)
提交回复
热议问题