vba中用正则表达式提取数值给数组,求表达式?

2025-04-06 21:16:29
推荐回答(1个)
回答1:

  • VBA Code(假设在 Excel 的活动工作表的 A1 单元格中存储着上述 Json 字符串):

Option Explicit

Sub ExtractJsonData()
    Dim objRegExp As Object
    Dim objMatch As Object
    Dim objMatches As Object
    
    Set objRegExp = CreateObject("VBScript.RegExp")
    
    With objRegExp
        .IgnoreCase = True
        .Global = True
        .Pattern = """issue"":""(\d+?)"",""opennum"":""(.*?)"""
        
        If .Test([A1]) Then
            Set objMatches = .Execute([A1])
            
            For Each objMatch In objMatches
                Debug.Print objMatch.SubMatches(0); objMatch.SubMatches(1)
            Next
        End If
    End With
End Sub
  • 运行结果:

表达式就是代码中的 Pattern 属性值