VBA 正则表达式和字符串处理

VBA Regex and String Handling

Q1 . 在 VBA 中,我正在从事 Web 抓取工作,我能够获取字符串并将其存储到变量中。该字符串看起来像这样:

x = "123434[STM]CompilationError_Lib.c23434[STM]LinkingError432122[STM]Null Pointer Exception"

我想做的是,我会定义一个数组,并将子字符串存储到数组的每个索引中。

arr[0] = 123434[STM]CompilationError_Lib.c

arr[1] = 23434[STM]LinkingError

arr[2] = 432122[STM]Null Pointer Exception

警告:可以有任意数量的子字符串。它并不总是三个。

我为此编写的正则表达式模式是:

myRegExp.Pattern = `"\d+[:].[A-Za-z]*.[A-Za-z._]*[^0-9]"`

但它只捕获第一个子字符串而不是所有三个子字符串matches.How我可以这样做吗?

所以我会回答你的第一个问题。我建议你将其他部分分成单独的问题,如评论中已经提到的那样。

Option Explicit
' Or add in Tools > References > VBScript Reg Exp for early binding
Public Sub testing()
    Dim x As String, arr() As String, i As Long, matches As Object
    x = "123434[STM]CompilationError_Lib.c23434[STM]LinkingError432122[STM]Null Pointer Exception"

    Static re As Object

    If re Is Nothing Then
        Set re = CreateObject("VBScript.RegExp")
        re.Global = True 'Don't know how you will deploy shown here for demo
        re.MultiLine = True 'Don't know how you will deploy shown here for demo
    End If

    re.IgnoreCase = False
    re.Pattern = "\d+\[[a-zA-Z]{3}][^0-9]+"
    Set matches = re.Execute(x)

    ReDim arr(0 To matches.Count - 1)
    For i = LBound(arr) To UBound(arr)
        arr(i) = matches(i)
    Next i
End Sub

输出: