+ Reply to Thread
Results 1 to 10 of 10

Regular Expression Help

Hybrid View

  1. #1
    Registered User
    Join Date
    01-26-2015
    Location
    Massachusetts
    MS-Off Ver
    2016
    Posts
    73

    Regular Expression Help

    Hello,

    I'm trying to use Regular Expression in a custom function to find two words and add a comma between them.

    I have the following code but the comma does not get added. I simply get "Avenue Apt." when I want to see "Avenue, Apt." etc.

    Thank you for your help!


    Function FIXAPARTMENT(strLookIn As String) As String
    Dim objRegEx

    Set objRegEx = CreateObject("VBScript.RegExp")
    objRegEx.Global = True
    objRegEx.IgnoreCase = False

    FIXAPARTMENT = strLookIn

    objRegEx.Pattern = "\bAvenue Apt.\b"
    FIXAPARTMENT = objRegEx.Replace(FIXAPARTMENT, "Avenue, Apt.")

    objRegEx.Pattern = "\bBoulevard Apt.\b"
    FIXAPARTMENT = objRegEx.Replace(FIXAPARTMENT, "Boulevard, Apt.")

    objRegEx.Pattern = "\bStreet Apt.\b"
    FIXAPARTMENT = objRegEx.Replace(FIXAPARTMENT, "Street, Apt.")

    END FUNCTION

  2. #2
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Regular Expression Help

    You have to include the ASCII value for the comma,
    I do not know why all that stuff about objRegEx and so.
    A comma can be interpreted as a separator and probably the \b (????) does this all too.
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  3. #3
    Registered User
    Join Date
    01-26-2015
    Location
    Massachusetts
    MS-Off Ver
    2016
    Posts
    73

    Re: Regular Expression Help

    Thanks for your help. But I'm still not sure how to enter it.

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Another way …

    Hi !

    Try this :

    PHP Code: 
    Sub Demo()
           
    S$ = "VBA Avenue Apt. test"
           
    P& = InStr(S"Avenue Apt.")
        If 
    P Then
            Mid
    (S6) = ","
            
    MsgBox S
        End 
    If
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

  5. #5
    Registered User
    Join Date
    01-26-2015
    Location
    Massachusetts
    MS-Off Ver
    2016
    Posts
    73

    Re: Regular Expression Help

    When I try escaping the comma with a backslash, I get the comma but also the backslash in the output!

    objRegEx.Pattern = "\bBoulevard Apt.\b"
    FIXAPARTMENT = objRegEx.Replace(FIXAPARTMENT, "Boulevard\, Apt.")

    Results: 50 Maple Boulevard\, Apt. 3

    I just want the comma to appear as literal text. Everything else is working!

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Regular Expression Help

    A period in a pattern is a Meta Character, so you need to escape with a back slush.

    i.e
    objRegEx.Pattern = "\bAvenue Apt\.\b"

  7. #7
    Registered User
    Join Date
    01-26-2015
    Location
    Massachusetts
    MS-Off Ver
    2016
    Posts
    73

    Re: Regular Expression Help

    Like I said, everything is working. It's finding the pattern properly, it's just not fixing it by adding a comma in the replacement output.

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Regular Expression Help

    Sub test()
        MsgBox FIXAPARTMENT("50 Maple Boulevard Apt. 3")
    End Sub
    
    Function FIXAPARTMENT(strLookIn As String) As String
        With CreateObject("VBScript.RegExp")
            .Pattern = "\b(Avenue|Boulevard|Street)(?= *Apt\.)"
            If .test(strLookIn) Then
                FIXAPARTMENT = .Replace(strLookIn, .Execute(strLookIn)(0).submatches(0) & ", ")
            End If
        End With
    End Function

  9. #9
    Registered User
    Join Date
    01-26-2015
    Location
    Massachusetts
    MS-Off Ver
    2016
    Posts
    73

    Re: Regular Expression Help

    Thanks Jindon. What would I add to accommodate other variations for Apt. like No., Unit, Ste., Bldg. etc. or sometimes it's just the number like:

    50 Maple Boulevard 3

    I still need the comma 50 Maple Boulevard, 3

    Thank you.

    Also, curious why I couldn't just add

    this & ", ")

    to the end of my code to get the comma like this.

    FIXAPARTMENT = objRegEx.Replace(FIXAPARTMENT, "Street & ",")

    I get a #VALUE error instead.

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Regular Expression Help

    If you upload a workbook with before/after, I can give you a suggestion.

    BTW, the way you are using Regular Expression is not effective.
    It would be much easier like
    Sub test()
        Dim e, FIXAPARTMENT As String
        FIXAPARTMENT = "Avenue Apt."
        For Each e In Array("Avenue", "Boulevard", "Street")
            If InStr(FIXAPARTMENT, e & " Apt.") Then
                FIXAPARTMENT = Application.Replace(FIXAPARTMENT, InStr(FIXAPARTMENT, " Apt."), , ",")
                Exit For
            End If
        Next
        MsgBox FIXAPARTMENT
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Regular expression
    By chief_abound in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-10-2015, 10:59 PM
  2. [SOLVED] Regular Expression pattern
    By BS Singh in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-13-2014, 10:53 PM
  3. Backreferencing In VBA Regular expression
    By BS Singh in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-11-2014, 11:46 PM
  4. Regular expression
    By Gourav in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-18-2014, 03:23 PM
  5. regular expression for password
    By Gourav in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-15-2014, 04:05 PM
  6. [SOLVED] Regular Expression
    By Kvramana in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-18-2013, 05:32 AM
  7. regular expression in vba is not working
    By johnmerlino in forum Excel General
    Replies: 2
    Last Post: 07-07-2011, 06:19 PM

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