CSV parsing with regex
-
I'm trying to parse a CSV string and examples I've found so far do not appear to work with VB.NET The regular expression I'm trying to work with is ,(?=([^"]*"[^"]*")*(?![^"]*")) So if I'm coding this in VB.NET I have to replace the double quotes with two double quotes right?.... Therefore, my code is using a simple form with two controls as follows... Dim re As Regex = New Regex(",(?=([^""]*""[^""]*"")*(?![^""]*""))", RegexOptions.Compiled) Dim myArray() As String = re.Split(TextBox1.Text) TextBox2.Clear() Dim i As Integer For i = 0 To UBound(myArray) TextBox2.Text = TextBox2.Text & myArray(i) & vbCrLf Next Now if TextBox1 is A,B,C it works as expected If any of the values have double quotes round them it all goes pear shaped. Eg. a,"substr1,substr2",3.14,"hello" comes out with 7 elements in my array a ,3.14,"hello" "substr1,substr2" 3.14,"hello" 3.14 "hello" "hello" this is doing my nut! I've been looking at reference books and still can't get it to work. Is there someone out there that's going to be my saviour?!?! cheers mf