Excel-VBA:根据正则表达式提取文本(字符串、内容)

'测试:将“abc@@eee”中的@@提取出来
Sub test()
    MsgBox GetValueByRegex("abc@@eee", "abc(.*?)eee")
End Sub

Function GetValueByRegex(ByVal OrgStr As String, ByVal PatternStr As String)
    Set re = CreateObject("VBScript.RegExp")
    re.Pattern = PatternStr
    re.Global = True
    re.IgnoreCase = False
    Set matchs = re.Execute(OrgStr)
    If matchs.Count > 0 Then
        GetValueByRegex = matchs(0).submatches(0)
    Else
        GetValueByRegex = ""
    End If
End Function

 

你可能感兴趣的:(VBA,OFFICE-EXCEL)