【VBA】正規表現パターンにマッチした文字列を抽出する方法【コピペでOK】
今回は、 Excel VBAで正規表現を使用した文字列の抽出方法をシェアします。
正規表現を使用した文字列の評価方法(正規表現パターンに合致するかのチェック)については、
この記事 で解説していますので参考にしてください。
ソースコード
早速ですが、ソースコードを以下に記載します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
Option Explicit ' テスト実行用プロシージャ Public Sub Test() ' 変数宣言 Dim mPattern As String Dim mTarget As String Dim result As Collection Dim text As Variant ' ================================================================================ ' テストケース① ' ================================================================================ ' 正規表現パターンを設定 mPattern = "([a-z0-9\._]+):([0-9]+)" ' 検索対象文字列を設定 mTarget = "blog.baseballyama.tokyo:80,dev.baseballyama.tokyo:8080" ' 検索実行 Set result = GetMatchedText(mPattern, mTarget, True, 0) ' 結果出力 Debug.Print "★テストケース①★" Debug.Print "正規表現パターン : " & mPattern Debug.Print "検索文字列 : " & mTarget If result.Count = 0 Then Debug.Print "検索結果 : " & "(マッチしませんでした)" Else For Each text In result Debug.Print "検索結果 : " & CStr(text) Next End If Debug.Print "" ' ================================================================================ ' テストケース② ' ================================================================================ ' 検索実行 Set result = GetMatchedText(mPattern, mTarget, True, 2) ' 結果出力 Debug.Print "★テストケース②★" Debug.Print "正規表現パターン : " & mPattern Debug.Print "検索文字列 : " & mTarget If result.Count = 0 Then Debug.Print "検索結果 : " & "(マッチしませんでした)" Else For Each text In result Debug.Print "検索結果 : " & CStr(text) Next End If Debug.Print "" ' ================================================================================ ' テストケース③ ' ================================================================================ ' 正規表現パターンを設定 mPattern = "([a-z0-9\._]+):([0-9]+)" ' 検索対象文字列を設定 mTarget = "全然マッチしない文字列" ' 検索実行 Set result = GetMatchedText(mPattern, mTarget, True, 1) ' 結果出力 Debug.Print "★テストケース③★" Debug.Print "正規表現パターン : " & mPattern Debug.Print "検索文字列 : " & mTarget If result.Count = 0 Then Debug.Print "検索結果 : " & "(マッチしませんでした)" Else For Each text In result Debug.Print "検索結果 : " & CStr(text) Next End If Debug.Print "" End Sub ' 正規表現パターンにマッチした文字列を取得 ' 第1引数 : 正規表現パターン ' 第2引数 : 検索対象文字列 ' 第3引数 : 大文字小文字を区別するか(True : 区別しない) ' 第4引数 : 取得するサブマッチグループ(全体を取得する場合は 0 を設定) Public Function GetMatchedText(ByRef pPattern As String, ByRef pTarget As String, ByRef pIgnoreCase As Boolean, ByRef pGroup As Long) As Collection ' 変数宣言 Dim regex As VBScript_RegExp_55.RegExp Dim match As VBScript_RegExp_55.match Dim subMatch As VBScript_RegExp_55.SubMatches Dim result As Collection ' 結果を初期化 Set result = New Collection ' 正規表現オブジェクト取得 Set regex = New VBScript_RegExp_55.RegExp With regex ' 正規表現パターンを設定 .Pattern = pPattern ' 大文字小文字の区別有無を設定 .IgnoreCase = pIgnoreCase ' 文字列全体を検索 .Global = True ' 検索文字列が正規表現パターンとマッチしなかった場合は処理終了 ' (空のCollectionを返却) If Not .Test(pTarget) Then Set GetMatchedText = result Exit Function End If ' マッチした文字列を走査 For Each match In .Execute(pTarget) ' サブマッチグループが 0 の場合はマッチした文字列全体を取得 If pGroup = 0 Then result.Add match.Value ' 「VBScript_RegExp_55.SubMatches」クラスのインデックスは 0 開始のため ' 引数の数値から 1 減らしてサブマッチを取得 Else Set subMatch = match.SubMatches result.Add subMatch.Item(pGroup - 1) End If Next End With ' 結果返却 Set GetMatchedText = result End Function |
上記の「 GetMatchedText 」 プロシージャを自分のプログラムにコピペすれば
正規表現パターンを使った文字列評価ができるようになります。
正規表現は間違いやすいので、事前にチェックすることをオススメします。
ココ で簡単なチェック方法をシェアしていますので参考にしてください。
上記で使用している VBScript_RegExp_55.RegExp については、
「参照設定」という作業を実施しないと使えません。(簡単です)
この方法については、 ココ でシェアしていますので、ここを参考に参照設定を実施してください。
まとめ
今回は、Excel VBA で正規表現を用いた文字列抽出をする方法をシェアしました。
Excel VBAについて他にもこんな処理はどうやるの?などの質問がある場合は、
フォームからご連絡頂けたら幸いです。
記事にして共有させて頂きます。