Execute
Searches a string for the pattern and returns the matches.
Syntax: object.Execute ( sourceString ) As MatchCollection
- object
- required An object expression that evaluates to a RegExp.
- sourceString
- required A String, the text to search.
Returns a MatchCollection that holds one Match for each match of Pattern in sourceString, in the order they occur. The collection is empty when there is no match.
Remarks
With Global set to False, the collection holds at most one match, the first. With Global set to True it holds every match.
IgnoreCase, MultiLine and DotAll change what counts as a match.
Each Match gives the matched text (Value), the position where it starts (FirstIndex, counted from 0), its length, and the text of each group of the pattern (SubMatches). A group written (?<name>...) can be read by its name.
An invalid pattern raises the compile error, with the number in LastErrorNumber and the description in LastError. Check IsValid first to avoid it.
Example
Dim re As RegExp
Set re = New RegExp
re.Pattern = "(?<year>\d{4})-(?<month>\d{2})"
re.Global = True
Dim matches As MatchCollection
Set matches = re.Execute("2026-10 and 2027-01")
Debug.Print matches.Count
Dim m As Match
For Each m In matches
Debug.Print m.Value & " at " & m.FirstIndex & ": " & m.SubMatches.Item("year") & "/" & m.SubMatches.Item("month")
Next
re.Global = False
Debug.Print re.Execute("2026-10 and 2027-01").Count
Debug.Print re.Execute("no date").Count
' Output:
' 2
' 2026-10 at 0: 2026/10
' 2027-01 at 12: 2027/01
' 1
' 0