Right now, the input is like a list of fields:
"field1", "field2", field3", ...
And each of those fields match up with a string in the fields
array based on the client's specifications.
I parse all of the fields and put them into a dictionary to use outside the function, the key being their corresponding name from the fields array. Then as a check if the input isn't way off base, I compare the length of the dictionary to the length of the fields array (+- 1 for a bit of flexibility).
Finally, the function returns True or False based on how "off" the input field length is from the fields array. The problem is the client will add additional fields like that may cause errors.
Is there a more flexible way to do this check to reduce the chance of these errors in the future?
Greatly appreciate your input.
Public Function parseCsv(ByVal csv As String, ByRef dict As Dictionary(Of String, String)) As Boolean
Dim fields As String() = New String() {"Patient_Name", "Date_of_Birth", "Address", "County"}
dict = New Dictionary(Of String, String)
Dim csvRegex As String = "(?:,""|^"")(""""|[\w\W]*?)(?="",|""$)|(?:,(?!"")|^(?!""))([^,]*?)(?=$|,)|(\r\n|\n)"
Dim fieldCount As Integer = 0
Try
Dim matches As MatchCollection = Regex.Matches(csv, csvRegex)
Dim m As Match
For index As Integer = 0 To matches.Count - 1
m = matches(index)
Dim fieldValue As String = ""
If m.Groups(1).ToString() IsNot "" Then
fieldValue = m.Groups(1).ToString()
ElseIf m.Groups(2).ToString() IsNot "" Then
fieldValue = m.Groups(2).ToString()
ElseIf m.Groups(3).ToString() IsNot "" Then
fieldValue = m.Groups(3).ToString()
End If
If index < fields.Length Then
dict.Add(fields(index), fieldValue)
End If
fieldCount += 1
Next
Catch ex As Exception
Return False
End Try
Return fieldCount = fields.Length Or fieldCount = fields.Length - 1 Or fieldCount = fields.Length + 1
End Function
Unfortunately, the input comes in as a big list of data and I can't control what it looks like.