VBAで正規表現HITした文字列取得方法

$1,$2みたいな取り方ね。

Sub RegExpSample4()
    Dim regEx As Object, Matches  As Object, Match As Object

    Set RE = CreateObject("VBScript.RegExp")

    Dim targetString As String
    targetString = "kakeru@yabuki @kyogokudo  hisao@nanao "

    With RE
        .Pattern = "([^@\s]+)@([^@\s]+)"
        .IgnoreCase = False
        .Global = True
    End With

    Set Matches = RE.Execute(targetString)

    For Each Match In Matches
        MsgBox Match.submatches(0) & ":" & Match.submatches(1)
        '' 1回目 kakeru:yabuki 、2回目 hisao:nanao
    Next Match
End Sub


ネタ元:VBAの正規表現における後方参照の考え方とサンプルコード

  • 15f9a95d14498f5476aa