對(duì)指定的字符串執(zhí)行正則表達(dá)式搜索。
object.Execute(string)
正則表達(dá)式搜索的設(shè)計(jì)模式是通過 RegExp 對(duì)象的 Pattern 來設(shè)置的。
Execute 方法返回一個(gè) Matches 集合,其中包含了在 string 中找到的每一個(gè)匹配的 Match 對(duì)象。如果未找到匹配,Execute 將返回空的 Matches 集合。
下面的代碼說明了 Execute 方法的用法。
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 ' 對(duì) Matches 集合進(jìn)行迭代。
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"))
應(yīng)用于:RegExp 對(duì)象