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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章