Name

Match Object

Description

A member of the Matches collection that is returned by a call to the RegExp object’s Execute method, the Match object represents a successful regular expression match.

Createable

No.

Returned by

Matches.Item property.

Properties

The Match object supports the following three properties:

FirstIndex

Data Type: Long

Indicates the position in the original search string where the regular expression match occurred. The first character in the search string is at position 1.

Length

Data Type: Long

Indicates the number of characters in the match found in the search string. This is also the number of characters in the Match object’s Value property.

Value

Data Type: String

The text of the match found in the search string.

Example

Since the RegExp object’s Execute method searches only a string, the example program writes the filename of each file in the Windows directory to a variable named strNames. Each filename is preceded by two spaces. The RegExp object’s Execute method is then called to search for every filename beginning with the letter “B” (the regular expression searches for two spaces followed by a “B”). The Matches collection is then iterated so that each filename can be extracted from strNames and displayed in a message box:

Dim fs, root, dir, fls, fl Dim rexp Dim mtchs, mtch Dim strNames, strMsg Dim lStartPos strNames = " " Set fs = CreateObject("Scripting.FileSystemObject") Set root = fs.Drives("C").RootFolder Set dir = root.SubFolders("Windows") Set fls = dir.Files For ...

Get VBScript in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.