THX1138
THX1138

Reputation: 1766

How to split on multiple delimiters in access vba using regular expressions

I am having issues figuring out how to split on multiple delimiters using regular expressions in access vba. I want to take a string say "This;is,really:annoying" and a tring user-defined delimiters separated by pipes (;|,) so that I would obtain the following result "This" , "is", "really:annoying" No matter what I do I cannot get this to work. In python I would just use re.split but vba has no such option that I know of. I have posted the code that I have tried:

 Private Sub Splitter(ByVal UnmodText As String, ByVal SplitDelimiters As String)
   Dim SplitExp As New RegExp
   Dim SplitMatches As MatchCollection
   Dim SplitMatch As Match
   SplitExp.IgnoreCase = True
   SplitExp.Global = True
   'SplitExp.Pattern = ".(" & SplitDelimiters & ")" & "|(?<=" & SplitDelimiters & ").$"
   'SplitExp.Pattern = ".{0,}(?=(" & SplitDelimiters & "))"
   SplitExp.Pattern = "(?!(" & SplitDelimiters & "){2})"
   MsgBox SplitExp.Pattern
   Set SplitMatches = SplitExp.Execute(UnmodText)
   For Each SplitMatch In SplitMatches
       MsgBox SplitMatch.Value
   Next
 End Sub

Any help would be greatly appreciated!

Upvotes: 1

Views: 4518

Answers (1)

Tim Williams
Tim Williams

Reputation: 166126

This will return a variant array given a string to split, and a string containing pipe-delimited separators.

Function SplitPlus(val As String, seps As String) As Variant
    Dim x As Integer, arrSeps As Variant
    Dim lb As Integer, ub As Integer

    arrSeps = Split(seps, "|")
    lb = LBound(arrSeps)
    ub = UBound(arrSeps)

    If ub > lb Then
        For x = lb + 1 To ub
            val = Replace(val, arrSeps(x), arrSeps(lb))
            'Debug.Print val
        Next x
    End If
    SplitPlus = Split(val, arrSeps(lb))
End Function

Upvotes: 3

Related Questions