I'm trying to write a Regex that that will extract individual fields from a CSV file.
For example, if given the following line in a CSV file:
123, Bob ,Bob, " Foo Bar ", "a, ""b"", c"
' Foo Bar '
'a, "b", c'
Well there are many gotchas and error possiable with Regexes... try following code it did trick for me and it is sweet and simple...
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
Mark as answer if found handy ...;)
Please see the same implementation here,,,