Splitting String in VBA using RegEx

前端 未结 2 1652
情书的邮戳
情书的邮戳 2020-11-30 13:20

I\'m new to VBA and would like to seek some help with regards to using RegEx and I hope somehow can enlighten me on what I\'m doing wrong. I\'m currently trying to split a d

2条回答
  •  醉酒成梦
    2020-11-30 13:41

    To split a string with a regular expression in VBA:

    Public Function SplitRe(Text As String, Pattern As String, Optional IgnoreCase As Boolean) As String()
        Static re As Object
    
        If re Is Nothing Then
            Set re = CreateObject("VBScript.RegExp")
            re.Global = True
            re.MultiLine = True
        End If
    
        re.IgnoreCase = IgnoreCase
        re.Pattern = Pattern
        SplitRe = Strings.Split(re.Replace(text, ChrW(-1)), ChrW(-1))
    End Function
    

    Usage example:

    Dim v
    v = SplitRe("a,b/c;d", "[,;/]")
    

提交回复
热议问题