VBA 正则表达式

'正则
Function RegExp(text As String, reg As String) As String
  
    Dim mRegExp As Object       '正则表达式对象
    Dim mMatches As Object      '匹配字符串集合对象
    Dim mMatch As Object        '匹配字符串
    
    RegExp = ""
    
    Set mRegExp = CreateObject("Vbscript.Regexp")
    With mRegExp
        .Global = True                              'True表示匹配所有, False表示仅匹配第一个符合项
        .IgnoreCase = True                          'True表示不区分大小写, False表示区分大小写
        .Pattern = reg  '匹配字符模式 ".*[款].*[号][\d]+[、](.*)"
        Set mMatches = .Execute(text)   '执行正则查找,返回所有匹配结果的集合,若未找到,则为空
        
        For Each mMatch In mMatches
            If (Not mMatch.SubMatches(0) = "") Then
                RegExp = Trim(mMatch.SubMatches(0))
            End If
        Next
    End With
    
    Set mRegExp = Nothing
    Set mMatches = Nothing
End Function

posted @ 2019-05-20 11:05  grj001  阅读(444)  评论(0编辑  收藏  举报