正則表達式 Match 對象的集合。
Matches 集合中包含若干獨立的 Match 對象,只能使用 RegExp 對象的 Execute 方法來創(chuàng)建之。與獨立的 Match 對象屬性相同,Matches `集合的一個屬性是只讀的。
在執(zhí)行正則表達式時,可能產生零個或多個 Match 對象。每個 Match 對象都提供了與正則表達式匹配的字符串的訪問入口、字符串的長度,以及標識匹配位置的索引。
下面的代碼將說明如何使用正則表達式查找獲得 Matches 集合,以及如何循環(huán)遍歷集合:
Function RegExpTest(patrn, strng) Dim regEx, Match, Matches ' 創(chuàng)建變量。 Set regEx = New RegExp ' 創(chuàng)建正則表達式。 regEx.Pattern = patrn ' 設置模式。 regEx.IgnoreCase = True ' 設置是否區(qū)分大小寫。 regEx.Global = True ' 設置全程匹配。 SetMatches
= regEx.Execute(strng) ' 執(zhí)行搜索。 For Each Match inMatches
' 循環(huán)遍歷Matches集合。 RetStr = RetStr & "Match found at position " RetStr = RetStr & Match.FirstIndex & ". Match Value is '" RetStr = RetStr & Match.Value & "'." & vbCRLF Next RegExpTest = RetStr End Function MsgBox(RegExpTest("is.", "IS1 is2 IS3 is4"))