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