Results 1 to 10 of 10

Use VBA and REGEX to Parse String

Threaded View

  1. #1
    Forum Contributor
    Join Date
    12-26-2010
    Location
    Kansas City, Kansas
    MS-Off Ver
    Excel 2007
    Posts
    147

    Use VBA and REGEX to Parse String

    Hi

    I have variations of the following type of string generated from a stat program:


    Equation: (On 1 Row)
    -------------------------------
    DLOG(DEM_B_C_TRP(-3)/1000+1)*.01/(.01+.24947)+DLOG(DEM_B_C_VT(-1)/1000+1)*.35947/(.01+.24947)


    I would like to pass a regex PATTERN to a VBA function such that the resultant
    output would look like this:

    (This output results in 3 rows)


    DLOG(DEM_B_C_TRP(-3)/1000+1)*.01/(.01+.24947)

    +

    DLOG(DEM_B_C_VT(-1)/1000+1)*.35947/(.01+.24947)


    ==========================================
    Whats important to *see* within the equation
    is that there are

    + signs WITHIN () and
    + signs NOT within ()


    and it is the + signs not within () that are the discrimenators
    that identify each _segment_ of the overall equation.


    ==========================================
    At present I am using the function below (found at a MS site)
    but I am not certain it is the correct function to use.

    More importantly, I just cannot figure out the correct pattern
    to employ that deals with definitions above.

    I have tried numerious patterns and my current one is

    =msfind(A22,"(DLOG)+[^Z.*]+.*")

    Bottom line - I just can't *see* the correct syntax. (This is the first
    time I have tried RegEx)

    Current Function:

    Note: I realize the current function could NOT return multiple
    rows but at this time I am trying to get it to just return
    the 1st segment

    
    'http://www.cs.cf.ac.uk/Dave/Internet/NEWS/regexp.html#simple
    
    Function MSFind(pFindIn As String, pFindWhat As String)
       
        'http://support.microsoft.com/kb/818802/en-us
    
    
       
        'Create objects.
        Dim oItem As Match
        Dim colMatches   As MatchCollection
        Dim sReturn As String
       
        '-------------------------
        ' Create a regular expression object.
        '-------------------------
        Dim R As New VBScript_RegExp_55.RegExp
    
        '-------------------------
        'Set the pattern by using the Pattern property.
        ' Set Case Insensitivity.
        'Set global applicability.
        '-------------------------
        R.Pattern = pFindWhat
        R.IgnoreCase = True
        R.Global = True
    
        '-------------------------
        'Test whether the String can be compared.
        '-------------------------
        If (R.Test(pFindIn) = True) Then
    
            '-------------------------
            'Get the matches.
            '-------------------------
            Set colMatches = R.Execute(pFindIn)   ' Execute search.
    
            '-------------------------
            'Get the matches.
            '-------------------------
            For Each oItem In colMatches   ' Iterate Matches collection.
              'sReturn = sReturn & "Match found at position "
              sReturn = sReturn & "P:" & oItem.FirstIndex & Space(2)
              sReturn = sReturn & "V:" & Space(1) & oItem.Value & Space(5)
            Next
        Else
             sReturn = "String Matching Failed"
        End If
       
        '-------------------------
        '-------------------------
        MSFind = sReturn
    
    End Function
    regards
    John
    Last edited by JohnM3; 11-19-2011 at 11:39 PM.

Thread Information

Users Browsing this Thread

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

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