need help with regular expressions wrapper function

Discussion in 'ASP General' started by mike, Oct 2, 2006.

  1. mike

    mike Guest

    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?
    mike, Oct 2, 2006
    #1
    1. Advertising

  2. mike

    mike Guest

    still trying to make a regular expressions wrapper function

    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

    %>
    mike, Oct 2, 2006
    #2
    1. Advertising

  3. Re: still trying to make a regular expressions wrapper function

    "mike" <> wrote in message
    news:...
    > 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.


    >
    > 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
    >
    > %>
    >
    Anthony Jones, Oct 3, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris R. Timmons

    Re: Help with regular expressions.

    Chris R. Timmons, Jul 4, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    391
    Chris R. Timmons
    Jul 4, 2003
  2. Jay Douglas
    Replies:
    0
    Views:
    594
    Jay Douglas
    Aug 15, 2003
  3. Replies:
    4
    Views:
    83
  4. .Net Sports
    Replies:
    2
    Views:
    101
    Evertjan.
    May 8, 2007
  5. Noman Shapiro
    Replies:
    0
    Views:
    222
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page