0

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.

resolute
  • 171
  • 2
  • 11
  • See https://stackoverflow.com/questions/736629/parse-delimited-csv-in-net – Wiktor Stribiżew Mar 26 '21 at 00:12
  • [CsvHelper](https://joshclose.github.io/CsvHelper/) – Jimi Mar 26 '21 at 00:29
  • Agree with Jimi - CSV parsing has been done to death on Nuget; rolling your own is about as pointful as writing your own version of VB.NET. Note that we're in the business of making software recomendations of particular packages, so please review https://www.nuget.org/packages?q=csv (not that we're in the business of finding resources for people either... :) ) – Caius Jard Mar 26 '21 at 06:39
  • ps; if it helps, don't bother being so rigid - just parse the whole CSV in whatever order it is, however many columns, and then pick out the columns you want wherever they are; it's why we have headers – Caius Jard Mar 26 '21 at 06:42

0 Answers0