Need help accessing the matches collection returned vbscript regex

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am using the vbscript 5.0 regex support - here is a snippet of the code I'm
having problems with:

dim m as integer
dim matched
dim match_coll

Set match_coll = regex.Execute( [some text string] )

' can be multiple matches in a single sentence, if global search...
For m = 1 To match_coll.Count

matched = match_coll(m)
..
..
..

During execution, I get an error on the "matched = match_coll(m)" statement
- it complains:


Runtime error '5':
Invalid procedure call or argument

Apparently it's being caused by the part of the statemnt that reads
"match_coll(m)". I figured that if match_coll were a collection, I should be
able to iterate through it by an index - why can't I? (I know I could "for
each...", but I need to be able to interrupt this loop and restart it from
where it was interrupted - I don't know how to do that with "for each")

Can anyone help? Thanks!
 
--
Dave Jenkins
K5KX


Dave Jenkins said:
I am using the vbscript 5.0 regex support - here is a snippet of the code I'm
having problems with:

dim m as integer
dim matched
dim match_coll

Set match_coll = regex.Execute( [some text string] )

' can be multiple matches in a single sentence, if global search...
For m = 1 To match_coll.Count

matched = match_coll(m)
.
.
.

During execution, I get an error on the "matched = match_coll(m)" statement
- it complains:


Runtime error '5':
Invalid procedure call or argument

Apparently it's being caused by the part of the statemnt that reads
"match_coll(m)". I figured that if match_coll were a collection, I should be
able to iterate through it by an index - why can't I? (I know I could "for
each...", but I need to be able to interrupt this loop and restart it from
where it was interrupted - I don't know how to do that with "for each")

I think I see part of the problem (at least I'm getting a little further
along in the code) - it appears that collections are indexed starting with
zero, and hence when I used an index of 1 to attempt to get the first element
of a 1-element collection, I was bombing out. Do you all agree?
 
Dave,

You might want to post this to the appropriate VB/VBScript newsgroup.

You've posted to a group that deals with PowerPoint.

But offhand, I'd suggest being more precise with variable definitions:
dim m as integer
dim matched
dim match_coll

Dim m As Long
Dim Matched as String ' I'd assume
Dim Match_Coll as Collection

And possibly change:
matched = match_coll(m)

to

Matched = Match_Coll(m).Value

All of the above are strictly WAGs.
 
Thanks, Steve, for the good advice.

I am using VBA out of PPT, and hence the posting here. I feel like I'm
having more of a VBA problem, than VBScript. You may be right, though: The
regex documentation (scanty) says that the Execute method returns a
Collection. But when I type the returned object as a Collection, it bombs at
runtime. Further, the fact that I have to index starting from 0 kind of
tells me that he's returning an array - what do you think? Aren't
collections always indexed base 1? (I do have Option Base 0 set - does that
apply to Collections?)

Thanks for lending an ear...
 
Ah, Dave, ya just had to go and set me off. <g>
Actually, thanks for asking. It was an interesting trip ...

There's some good info here:
http://authors.aspalliance.com/brettb/VBScriptRegularExpressions.asp

And tons here:
http://msdn.microsoft.com/library/default.asp?URL=/library/en-us/dnclinic/html/s
cripting051099.asp

And this seems to fly:

Sub RegExAmple()
' Assumes that you've set a reference to
' Microsoft VBScript Regular Expressions
' I used 5.5 for this example, but earlier verions should work

Dim oRegex As RegExp
Dim cMatches As MatchCollection
Dim oMatch As Match

Set oRegex = New RegExp

With oRegex
.Pattern = "Find"
.IgnoreCase = False
.Global = True
End With

Set cMatches = oRegex.Execute("you'll find Find here, but only once")

If cMatches.Count > 0 Then
For Each oMatch In cMatches
Debug.Print oMatch.Value
Next
Else
Debug.Print oRegex.Pattern & " was not found in the string"
End If

Set oRegex = Nothing

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top