SubMatches Collection

 

Collection of regular expression submatch strings.

Remarks

A SubMatches collection contains individual submatch strings, and can only be created using the Execute method of the RegExp object. The SubMatches collection's properties are read-only

When a regular expression is executed, zero or more submatches can result when subexpressions are enclosed in capturing parentheses. Each item in the SubMatches collection is the string found and captured by the regular expression.

The following code illustrates how to obtain a SubMatches collection from a regular expression search and how to access its individual members:

Function SubMatchTest(inpStr)
  Dim oRe, oMatch, oMatches
  Set oRe = New RegExp
  ' Look for an e-mail address (not a perfect RegExp)
  oRe.Pattern = "(\w+)@(\w+)\.(\w+)"
  ' Get the Matches collection
  Set oMatches = oRe.Execute(inpStr)
  ' Get the first item in the Matches collection
  Set oMatch = oMatches(0)
  ' Create the results string.
  ' The Match object is the entire match - someone@example.com
  retStr = "Email address is: " & oMatch & vbNewline
  ' Get the sub-matched parts of the address.
  retStr = retStr & "Email alias is: " & oMatch.SubMatches(0)  ' someone
  retStr = retStr & vbNewline
  retStr = retStr & "Organization is: " & oMatch. SubMatches(1)' example
  SubMatchTest = retStr
End Function

MsgBox(SubMatchTest("Please send mail to someone@example.com. Thanks!"))

Requirements

Version 5.5

See Also

For Each...Next Statement
Match Object
Matches Collection
Regular Expression (RegExp) Object
Regular Expression Programming (Scripting)