franklin
franklin

Reputation: 1819

regular expressions and vba

Does anyone know how to extract matches as strings from a RegExp.Execute() function?

Let me show you what I've gotten to so far:

Regex.Pattern = "^[^*]*[*]+"
Set myMatches = Regex.Execute(temp)

I want the object "myMatches" which is holding the matches, to be converted to a string. I know that there is only going to be one match per execution.

Does anyone know how to extract the matches from the object as Strings to be displayed lets say via a MsgBox?

Upvotes: 0

Views: 3718

Answers (2)

Reafidy
Reafidy

Reputation: 8431

Try this:

Dim sResult As String

'// Your expression code here...

sResult = myMatches.Item(0)
'// or
sResult = myMatches(0)

Msgbox("The matching text was: " & sResult)

The Execute method returns a match collection and you can use the item property to retrieve the text using an index.

As you stated you only ever have one match then the index is zero. If you have more than one match you can return the index of the match you require or loop over the entire collection.

Upvotes: 2

Toby Allen
Toby Allen

Reputation: 11213

This page has a lot of information on regex and seems to have what you want.

http://www.regular-expressions.info/vbscript.html

Upvotes: 0

Related Questions