Matches 集合
正則表達(dá)式 Match 對(duì)象的集合。
說明
Matches 集合中包含若干獨(dú)立的 Match 對(duì)象,只能使用 RegExp 對(duì)象的 Execute 方法來創(chuàng)建之。與獨(dú)立的 Match 對(duì)象屬性相同,Matches `集合的一個(gè)屬性是只讀的。
在執(zhí)行正則表達(dá)式時(shí),可能產(chǎn)生零個(gè)或多個(gè) Match 對(duì)象。每個(gè) Match 對(duì)象都提供了與正則表達(dá)式匹配的字符串的訪問入口、字符串的長(zhǎng)度,以及標(biāo)識(shí)匹配位置的索引。
下面的代碼將說明如何使用正則表達(dá)式查找獲得 Matches 集合,以及如何循環(huán)遍歷集合:
Function RegExpTest(patrn, strng) Dim regEx, Match, Matches '
創(chuàng)建變量。 Set regEx = New RegExp '
創(chuàng)建正則表達(dá)式。 regEx.Pattern = patrn '
設(shè)置模式。 regEx.IgnoreCase = True '
設(shè)置是否區(qū)分大小寫。 regEx.Global = True '
設(shè)置全程匹配。 Set Matches = regEx.Execute(strng) '
執(zhí)行搜索。 For Each Match in Matches
'
循環(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"))