need help with regular expressions wrapper function

M

mike

Hello,

I'm trying to write a function to return submatches. I want to do this
because I want to run a match on matches of matches of yet other
matches, so it seemed to me it would be easier to work with if there
was one line of code that would return a collection or array of
matches. Here's what I got:

<%
Dim objRegExp
Set objRegExp = New RegExp
Dim objMatches, objMatch

Set submatches = getsubmatches("this should hopefully match and print
eight words", "(\w)", TRUE, TRUE)

For i = 0 To submatches.Count - 1
response.write "<br>" & submatches(i)
Next


Function getsubmatches(strText, pattern, isGlobal, ignoreCase)
Dim data

'Set our pattern
objRegExp.Pattern = pattern
objRegExp.IgnoreCase = ignoreCase
objRegExp.Global = isGlobal

For Each objMatch in objRegExp.Execute(strText)
getsubmatches = objMatch.SubMatches
Next
End Function

%>

I get:
Microsoft VBScript runtime (0x800A01C2)
Wrong number of arguments or invalid property assignment
/getsubmatches.asp, line 22

thats this line: "getsubmatches = objMatch.SubMatches"

Can anyone help me write this correctly?
 
M

mike

Heres my source. I don't understand why the text function works but
the object one does not. Can anyone help me?

regards,
Mike

<%

'this one works
response.write "<br>" & getSubmatchesText("this should hopefully match
and print eight words", "(\w)", TRUE, TRUE)

'this one doesn't work
Set submatches = getsubmatchesObject("this should hopefully match and
print eight words", "(\w)", TRUE, TRUE)
For i = 0 To submatches.Count - 1
response.write "<br>" & submatches(i)
Next


Function getsubmatchesObject(strText, pattern, isGlobal, ignoreCase)
Dim objRegExp
Set objRegExp = New RegExp
Dim objMatches, objMatch

'Set our pattern
objRegExp.Pattern = pattern
objRegExp.IgnoreCase = ignoreCase
objRegExp.Global = isGlobal

For Each objMatch in objRegExp.Execute(strText)
getsubmatchesObject = objMatch.SubMatches
Next
End Function


Function getSubmatchesText(strText, pattern, isGlobal, ignoreCase)
Dim objRegExp
Set objRegExp = New RegExp
Dim objMatches, objMatch

Dim data

'Set our pattern
objRegExp.Pattern = pattern
objRegExp.IgnoreCase = ignoreCase
objRegExp.Global = isGlobal

For Each objMatch in objRegExp.Execute(strText)
For i = 0 To objMatch.submatches.Count - 1
data = data & objMatch.SubMatches(i)
Next
Next
getSubmatchesText = data
End Function

%>
 
A

Anthony Jones

mike said:
Heres my source. I don't understand why the text function works but
the object one does not. Can anyone help me?

regards,
Mike

<%

'this one works
response.write "<br>" & getSubmatchesText("this should hopefully match
and print eight words", "(\w)", TRUE, TRUE)

'this one doesn't work
Set submatches = getsubmatchesObject("this should hopefully match and
print eight words", "(\w)", TRUE, TRUE)
For i = 0 To submatches.Count - 1
response.write "<br>" & submatches(i)
Next


Function getsubmatchesObject(strText, pattern, isGlobal, ignoreCase)
Dim objRegExp
Set objRegExp = New RegExp
Dim objMatches, objMatch

'Set our pattern
objRegExp.Pattern = pattern
objRegExp.IgnoreCase = ignoreCase
objRegExp.Global = isGlobal

For Each objMatch in objRegExp.Execute(strText)
getsubmatchesObject = objMatch.SubMatches

A SubMatches is an object hence the line above should use a Set
Next
End Function

This code (when the set is applied) will return a submatches object
containing a single value which is the last word in the string. The
function doesn't really make any sense. It seems to me what might make more
sense is simply to return the resulting object from the execute method.
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top