The Code Garage – SplitQuoteComma

Filed under Code Garage

Today, I’m restarting my (well, actually I’ve seen something similar around the web) “Code Garage” concept. Basically, the idea is to post (hopefully regularly) some snippet of code that I’ve either come across, translated from some other language, or whatever and that I find myself using pretty consistently. I’ll tag them all with the “Code Garage” tag in my tag cloud.

Today, it’s a SplitQuoteComma function. VB has long had a split function, in one incarnation or another, but occassionally, you have to deal with quote/comma delimited text (i.e. text input that is comma delimited unless there’s a comma in the text, in which case the text is wrapped in quotes).

This kind of function is pretty trivial stuff, really, but I happened across a regex that makes it even trivialer (is that a word?):

    Public Function SplitQuoteComma(ByVal Args As String) As String()
        Dim r = New System.Text.RegularExpressions.Regex(",(?=(?:[^""]*""[^""]*"")*(?![^""]*""))")
        Return r.Split(Args)
    End Function

Post a Comment

Your email is never published nor shared. Required fields are marked *