excel支持正則表達式提取字符函數(支持RegExp捕獲分組)

1、要讓excel腳本支持Microsoft VBScript Regular Expressions 5.5 ,按快捷鍵alt+F11,出現下圖界面,操做如圖示:正則表達式

二.添加VBA代碼:express

 代碼添加完畢後,關閉該窗口。函數

 

 

Function regex(strInput As String, matchPattern As String, Optional ByVal outputPattern As String = "$0") As Variant
    Dim inputRegexObj As New VBScript_RegExp_55.RegExp, outputRegexObj As New VBScript_RegExp_55.RegExp, outReplaceRegexObj As New VBScript_RegExp_55.RegExp
    Dim inputMatches As Object, replaceMatches As Object, replaceMatch As Object
    Dim replaceNumber As Integer

    With inputRegexObj
        .Global = True
        .MultiLine = True
        .IgnoreCase = False
        .Pattern = matchPattern
    End With
    With outputRegexObj
        .Global = True
        .MultiLine = True
        .IgnoreCase = False
        .Pattern = "\$(\d+)"
    End With
    With outReplaceRegexObj
        .Global = True
        .MultiLine = True
        .IgnoreCase = False
    End With

    Set inputMatches = inputRegexObj.Execute(strInput)
    If inputMatches.Count = 0 Then
        regex = False
    Else
        Set replaceMatches = outputRegexObj.Execute(outputPattern)
        For Each replaceMatch In replaceMatches
            replaceNumber = replaceMatch.SubMatches(0)
            outReplaceRegexObj.Pattern = "\$" & replaceNumber

            If replaceNumber = 0 Then
                outputPattern = outReplaceRegexObj.Replace(outputPattern, inputMatches(0).Value)
            Else
                If replaceNumber > inputMatches(0).SubMatches.Count Then
                    'regex = "A to high $ tag found. Largest allowed is $" & inputMatches(0).SubMatches.Count & "."
                    regex = CVErr(xlErrValue)
                    Exit Function
                Else
                    outputPattern = outReplaceRegexObj.Replace(outputPattern, inputMatches(0).SubMatches(replaceNumber - 1))
                End If
            End If
        Next
        regex = outputPattern
    End If
End Function

 VBA腳本代碼參數說明:翻譯

  1. A text to use the regular expression on.(第一個參數爲被應用的字符串,即要從中提取的長字符串)
  2. A regular expression.(第二個參數爲匹配的正則表達式,外側須要加「」,支持捕獲分組)
  3. A format string specifying how the result should look. It can contain $0$1$2, and so on. $0 is the entire match, $1 and up correspond to the respective match groups in the regular expression. Defaults to $0.(第三個參數爲要捕獲字符的字符分組,「$0」表示所有匹配捕獲分組,「$1」,"$2"......表示捕獲分組序號)

函數調用示例:3d

 

=regex("Peter Gordon: some@email.com, 47", "^(.+): (.+), (\d+)$", "$1")
結果:
 Peter Gordon 

  

  教程翻譯自:https://stackoverflow.com/a/28176749/2109599excel

相關文章
相關標籤/搜索