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