Consulting

Results 1 to 3 of 3

Thread: VBA Regex

  1. #1
    VBAX Regular
    Joined
    Mar 2016
    Location
    Oslo
    Posts
    19
    Location

    Smile VBA Regex

    Hi, I am new to Regex, and struggeling to learn. In the meantime I reach out in hope for help.

    I am working with an excel-app that sends out requests for public information (FOIA) to public offices. The requests does always contain the case and documentnumber that identifies the documents at the public office. They are always in formats like this:


    2016/4408-95 KS Regnskapsundersøkelse 2016


    Dato28.02.2017SakOslo kommunes regnskap 2016FraKS



    2017/698-4 15/2017 Spørsmål om tilknytningsavgiften for vann og avløp


    Dato27.02.2017SakTilknytningsavgiftene for vann og avløpTilFinanskomiteen



    2016/4928-3 14/2017 Verbalvedtak og takster fra bystyrets behandling av budsjett 2017


    Dato27.02.2017SakOppfølging av budsjett 2017TilFinanskomiteen



    2016/6013-2 16/2017 Spørsmål til byrådet fra Peter Myhre(F) - Budsjettbehandling


    Dato27.02.2017SakKostnader ved budsjettbehandlingenTilFinanskomiteen



    2017/1115-2 Etterlyser urevidert regnskap - innsyn


    Dato24.02.2017SakInnsyn i regnskapet - økonomisk oversikt drift, investering og balanse 2016FraKommunal rapport AS



    2016/2167-11 Oversendelse av uavhengig revisors beretning om kompensasjonsoppgave for merverdiavgift for 6. termin 2016

    So thats: always 4 digits identifying the year, and then always "/", followed by x number of numbers, then "-" and finally x numbers of numbers.

    My excel-app extraxts each of this posts as one text-string. Could any help me write the regex - snippet I need to extract the casenumbers? The reason that I need Regex for it, is that they may appear in the middle of the text as well.

    Any help much appreciated
    Last edited by vegard_fv; 03-07-2017 at 05:54 AM. Reason: formatting

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Maybe something like this

    It only returns the first one, but can be expanded to return all 'Finds' in an array


    Option Explicit
    Sub drv()
        Dim s As String
        
        s = "2016/2167-11 Oversendelse av uavhengig revisors beretning om kompensasjonsoppgave for merverdiavgift for 6. termin 2016"
        MsgBox RegExpExtract(s)
    
        s = "Oversendelse av uavhengig revisors beretning om 2016/2167123456-1122334455 kompensasjonsoppgave for merverdiavgift for 6. termin 2016"
        MsgBox RegExpExtract(s)
    End Sub
    
    Function RegExpExtract(LookIn As String) As String
        Dim oRegExp As Object
        'Create instance of RegExp object
        Set oRegExp = CreateObject("VBScript.RegExp")
        With oRegExp
            .Pattern = "\d{4}/\d+-\d+"
            .Global = True
            .IgnoreCase = False
        End With
         
        'Test to see if there are any matches
        If oRegExp.test(LookIn) Then
             'Run RegExp to get the matches, which are returned as a zero-based collection
            RegExpExtract = oRegExp.Execute(LookIn)(0)
        Else
            RegExpExtract = vbNullString
        End If
    
        Set oRegExp = Nothing
    
    End Function
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Regular
    Joined
    Mar 2016
    Location
    Oslo
    Posts
    19
    Location
    Great, it works! Tthanks for giving me great head start. Super - cool!
    And I'll need to work on my Regex.

Posting Permissions

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