VBScript » Objects » Matches Collection

Version: 5.0

The Matches Collection is a collection of objects that contains the results of a search and match operation that uses a regular expression.

Simply put, a regular expression is a string pattern that you can compare against all or a portion of another string. However, in all fairness, be warned that regular expressions can get very complicated.

The RegExp object can be used to search for and match string patterns in another string. A Match object is created each time the RegExp object finds a match. Since, zero or more matches could be made, the RegExp object actually return a collection of Match objects which is refered to as a Matches collection.

Examples

Code:
<%
'this sub finds the matches
Sub RegExpTest(strMatchPattern, strPhrase)
'create variables
Dim objRegEx, Match, Matches, StrReturnStr
'create instance of RegExp object
Set objRegEx = New RegExp

'find all matches
objRegEx.Global = True
'set case insensitive
objRegEx.IgnoreCase = True
'set the pattern
objRegEx.Pattern = strMatchPattern

'create the collection of matches
Set Matches = objRegEx.Execute(strPhrase)

'print out all matches
For Each Match in Matches
strReturnStr = "Match found at position "
strReturnStr = strReturnStr Match.FirstIndex ". Match
Value is '"
strReturnStr = strReturnStr Match.value "'."
"<BR>" VBCrLf
'print
Response.Write(strReturnStr)
nbsp:Next
End Sub

'call the subroutine
RegExpTest "is.", "Is1 is2 Is3 is4"
%>
Output:
Match found at position 0. Match Value is 'Is1'.
Match found at position 4. Match Value is 'is2'.
Match found at position 8. Match Value is 'Is3'.
Match found at position 12. Match Value is 'is4'.
Explanation:

This code is a simplier, working version of a program published by Microsoft. Note how the For Each ... Next statement is used to loop through the Matches collection.

Properties

Count

Syntax: object.Count

Returns an integer that tells us how many Match objects there are in the Matches Collection. Syntax: object.

Item

Syntax: object.Item(key) [ = itemvalue]

The Item property allows us to retreive the value of an item in the collection designated by the specified key argument and also to set that value by using itemvalue.

Events

Initialize

See Also: