SubMatches Property

Returns the submatches of a Match Object object.




The object argument is always a Match object.


A SubMatches collection contains individual submatch strings, and can be created only by 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 the SubMatches property 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 email 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 -
  retStr = "Email address is: " & oMatch & vbNewline
  ' Get the sub-matched parts of the address.
  ' someone
  retStr = retStr & "Email alias is: " & oMatch.SubMatches(0)  
  retStr = retStr & vbNewline
  ' example 
  retStr = retStr & "Organization is: " & oMatch. SubMatches(1) 
  SubMatchTest = retStr
End Function

MsgBox(SubMatchTest("Please send mail to Thanks!"))


Version 5

Applies To: Match Object

Change History




March 2009

Reformatted code in example.

Information enhancement.

See Also

FirstIndex Property
Length Property
Value Property (VBScript)