Results 1 to 8 of 8

Return text with regex find function

Threaded View

  1. #1
    Registered User
    Join Date
    11-11-2008
    Location
    Syracuse NY
    MS-Off Ver
    2007
    Posts
    90

    Return text with regex find function

    In VBA , I'm trying to return the result of a regex find to my own sub with the following function. The problem is, I can't figure out what it's returning, or how to get it to return the actual text of the match. I also need to test if there is or isn't a match.

    The function, from this page ( http://www.tmehta.com/regexp/add_code.htm ) is reproduced here for convenience:

    Option Explicit
    #Const LateBind = True
    
    Function RegExpFind(FindIn, FindWhat As String, _
            Optional IgnoreCase As Boolean = False)
        Dim i As Long
        #If Not LateBind Then
        Dim RE As RegExp, allMatches As MatchCollection, aMatch As Match
        Set RE = New RegExp
        #Else
        Dim RE As Object, allMatches As Object, aMatch As Object
        Set RE = CreateObject("vbscript.regexp")
            #End If
        RE.Pattern = FindWhat
        RE.IgnoreCase = IgnoreCase
        RE.Global = True
        Set allMatches = RE.Execute(FindIn)
        ReDim rslt(0 To allMatches.Count - 1)
        For i = 0 To allMatches.Count - 1
            rslt(i) = allMatches(i).Value
            Next i
        RegExpFind = rslt
        End Function
    This function seems to allow for the possibility of finding more than one match at a time, which is not what I want.

    Here is an example of how I'm trying to use it. TwoWaySplitNum is a string variable.

    TwoWaySplitNum = RegExpFind(SplitTxt, "\d\d/\d\d")
    There's also a RegExpSubstitute function on that page if you're interested.
    Last edited by jrussell; 05-07-2010 at 08:29 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1