Visual Basic Scripting Edition | 語(yǔ)言參考 |
正則表達(dá)式子匹配字符串的集合。
SubMatches 集合包含了單個(gè)的子匹配字符串,只能用 RegExp 對(duì)象的 Execute 方法創(chuàng)建。SubMatches 集合的屬性是只讀的。
運(yùn)行一個(gè)正則表達(dá)式時(shí),當(dāng)圓括號(hào)中捕捉到子表達(dá)式時(shí)可以有零個(gè)或多個(gè)子匹配。SubMatches 集合中的每一項(xiàng)是由正則表達(dá)式找到并捕獲的的字符串。
下面的代碼演示了如何從一個(gè)正則表達(dá)式獲得一個(gè) SubMatches 集合以及如何它的專(zhuān)有成員:
Function SubMatchTest(inpStr) Dim oRe, oMatch, oMatches Set oRe = New RegExp '
查找一個(gè)電子郵件地址(不是一個(gè)理想的RegExp
)oRe.Pattern = "(\w+)@(\w+)\.(\w+)"
'
得到Matches
集合Set oMatches = oRe.Execute(inpStr)
'
得到Matches
集合中的第一項(xiàng)Set oMatch = oMatches(0)
'
創(chuàng)建結(jié)果字符串。' Match
對(duì)象是完整匹配—
dragon@xyzzy.com
retStr = "
電子郵件地址是:" & oMatch & vbNewline
'
得到地址的子匹配部分。retStr = retStr & "
電子郵件別名是:" & oMatch.SubMatches(0) ' dragon
retStr = retStr & vbNewline
retStr = retStr & "
組織是:" & oMatch. SubMatches(1)' xyzzy
SubMatchTest = retStr
End Function
MsgBox(SubMatchTest("
請(qǐng)寫(xiě)信到dragon@xyzzy.com
。謝謝!
"))
For Each...Next 語(yǔ)句 | Match 對(duì)象 | Matches 集合 |RegExp 對(duì)象