VBS tutorial: objects – match objects


Match object

Provides access to read-only properties that match regular expressions.


MatchObjects can only pass throughRegExpObjectExecuteMethod, which actually returnsMatchA collection of objects. be-allMatchObject properties are read-only.

When a regular expression is executed, it may produce zero or moreMatchObject. eachMatchObject provides access to the string found by regular expression search, the length of the string, and the index position to find a match.

The following code illustratesMatchObject usage:

Function RegExpTest(patrn, strng)  Dim regEx, Match, Matches          '  Create variables. Set regEx = New RegExp          '  Establish regular expressions. regEx.Pattern = patrn          '  Set the mode. regEx.IgnoreCase = True          '  Sets whether the is case sensitive. regEx.Global = True             '  Set global overrides. Set Matches = regEx.Execute(strng)       '  Perform a search. For Each Match in Matches          '  Traverse the matches collection. RetStr = RetStr & "Match " & I & " found at position "    RetStr = RetStr & Match.FirstIndex & ". Match Value is "'    RetStr = RetStr & Match.Value & "'." & vbCRLF  Next  RegExpTest = RetStrEnd FunctionMsgBox(RegExpTest("is.", "IS1 is2 IS3 is4"))