+ Reply to Thread
Results 1 to 11 of 11

Regular Expressions in VBA

  1. #1
    Registered User
    Join Date
    10-28-2006
    Posts
    6

    Regular Expressions in VBA

    I am trying to do some regular expressions with replace in VBA for Excel. But I'm unsure how to enter a match while excluding some characters.

    The original string is a URL like this:

    //lena/sasweb/cgi-bin/broker?_SERVICE=pool4&_PROGRAM=appsas1.MERCHANT.sas&EMAIL=&_DEBUG=0&FILE=MERCHANT&CIM=&DIV=&CIA=&ENTITY=43&FDEPT=&SUBD=772&FSUB=&BUYER=&FCAT=&SUPPLIER=&LOTSET=&BRAND=&LABEL=9

    I'd like to change where it says "ENTITY=43" to "ENTITY=45". All the variable are between "&" symbols. So how do I say in RegEx to replace just this one variable?

    Right now I'm using:
    &ENTITY=\w+
    And while it works it is not optimal. I'd like to match between the "&" symbols (or end of line) but not include them in the replacement.

    --------------------------------------------------
    Right now I'm using:

    Dim reg As New RegExp
    PatternStr = "&ENTITY=\w+"
    ReplacementStr = "&ENTITY=45
    reg.Pattern = PatternStr
    reg.IgnoreCase = True
    URLStr = reg.Replace(URLStr, ReplacementStr)

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Regular Expressions in VBA

    Hi,

    Do you really need VBA? Can't you just use a straightforward SUBSTITUTE() function? e.g.
    Please Login or Register  to view this content.
    Regards
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Regular Expressions in VBA

    or

    Please Login or Register  to view this content.



  4. #4
    Registered User
    Join Date
    10-28-2006
    Posts
    6

    Re: Regular Expressions in VBA

    The routines I'm making arn't always that simple. I just gave a very simple example. What if it says "&ENTITY=9934" or even "&ENTITY=&" which signifies no number entered. And I'm working with a much longer and more complicated URL with many more variables.

    I need to do something like this using regular expressions:
    (Match on this)(Just replace this and nothing more)(Match here also)

    Any ideas?

  5. #5
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Regular Expressions in VBA

    Apparently you overlooked my post.

  6. #6
    Registered User
    Join Date
    10-28-2006
    Posts
    6

    Re: Regular Expressions in VBA

    SNB:
    I love your post! It works good. I just thought there might be a way to use Regular Expressions to do it.

  7. #7
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Regular Expressions in VBA

    I prefer 'Irregular Expressions'.....

  8. #8
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Regular Expressions in VBA

    I am not a regexp guru but this seems simply enough. Maybe I don't have snb's split routine coded properly.

    Please Login or Register  to view this content.

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Regular Expressions in VBA

    Hello samualt,

    Welcome to the Forum!

    To answer you question about doing it with Regular Expressions, it is quite easy.
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  10. #10
    Registered User
    Join Date
    10-28-2006
    Posts
    6

    Re: Regular Expressions in VBA

    Leith:
    Your code works but fails if the original string has something like
    Please Login or Register  to view this content.
    which has a "B" after the number in entity. That is why my original code at the top uses /w+
    However, even that will break under certain circumstances.

    I need something like this:
    (Match on this)(Just replace this and nothing more)(Match here also)
    (&Entity=)(Replace whatever is right here before the next ampersand)(&)

  11. #11
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Regular Expressions in VBA

    Hello samualt,

    Use this pattern...
    Please Login or Register  to view this content.

+ Reply to Thread

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