SubMatches 集合

正则表达式子匹配字符串的集合

说明

SubMatches 集合包含了单个的子匹配字符串,只能用 RegExp 对象的 Execute 方法创建。SubMatches 集合的属性是只读的。

运行一个正则表达式时,当圆括号中捕捉到子表达式时可以有零个或多个子匹配。SubMatches 集合中的每一项是由正则表达式找到并捕获的的字符串。

下面的代码演示了如何从一个正则表达式获得一个 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) ' 创建结果字符串。
 ' Match 对象是完整匹配  dragon@xyzzy.com retStr ="电子邮件地址是: " & oMatch & vbNewline ' 得到地址的子匹配部分。
 retStr =retStr & "电子邮件别名是: " & oMatch.SubMatches(0) ' dragon retStr =retStr & vbNewline retStr =retStr & "组织是: " & oMatch. SubMatches(1)' xyzzy SubMatchTest =retStrEnd FunctionMsgBox(SubMatchTest("请写信到 dragon@xyzzy.com  谢谢!"))

属性

Count 属性 | Item 属性

要求

版本 5.5

请参阅

For Each...Next 语句 | Match 对象 | Matches 集合 |RegExp 对象

返回首页