+ Reply to Thread
Results 1 to 8 of 8

Regex Pattern Matching

  1. #1
    Registered User
    Join Date
    06-21-2018
    Location
    Calgary, AB
    MS-Off Ver
    2013
    Posts
    94

    Regex Pattern Matching

    Hi folks,

    I'm well acquainted with pattern matching using the str functions in VBA.

    I'm wondering, however, about the viability of using regex matches instead when running through multiple ranges for the same pattern.

    I'm VERY green with regex so please forgive my ignorance - if I wanted to match the following, how would it be done?

    -Length must be 10 characters
    - First 4 characters are letters only (no numbers)
    - IF the length is not 10 digits, insert a 0 AFTER the first 4 letters for every missing digit

    i.e. if I had ABCD01234 -> ABCD001234
    DCBA87623 -> DCBA087623
    WXYZ2354 -> WXYZ002354

    What would this look like?

    Thanks in advance.
    never stop learning. or milk

  2. #2
    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: Regex Pattern Matching

    Hello visualnotsobasic,

    Here is a macro that checks the string for 4 alpha characters (case is ignored) and then must be followed by 1 to 6 digits. Additional leading zeros are added if there are less than 6 digits.

    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!)

  3. #3
    Registered User
    Join Date
    06-21-2018
    Location
    Calgary, AB
    MS-Off Ver
    2013
    Posts
    94

    Re: Regex Pattern Matching

    Hi Leith,

    Sorry for my late reply!
    It looks fantastic, but I for some reason can not figure out how to get it to run on a specific range.

  4. #4
    Registered User
    Join Date
    06-21-2018
    Location
    Calgary, AB
    MS-Off Ver
    2013
    Posts
    94

    Re: Regex Pattern Matching

    Maybe just to clarify a bit more - if I have a named range, how do I run the regex match and replace it with the proper string on each cell?

    I apologize for my ignorance. I'm very new to this and I've been fooling around with your code for a few hours, but I can't seem to get it to do anything.

  5. #5
    Valued Forum Contributor
    Join Date
    02-06-2014
    Location
    N/A
    MS-Off Ver
    N/A
    Posts
    373

    Re: Regex Pattern Matching

    Hiya, I believe this should work for you.. (Borrowing from Leith)

    Please Login or Register  to view this content.

  6. #6
    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: Regex Pattern Matching

    Hello visualnotsobasic,

    Since you did not say how many cells there were, I optimized the code for speed and a large data set. Add a new standard VBA module to your workbook then copy and paste the code below into it. Be sure to change the name of the named range in the macro "Test" before you run the macro.

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    06-21-2018
    Location
    Calgary, AB
    MS-Off Ver
    2013
    Posts
    94

    Re: Regex Pattern Matching

    Absolutely brilliant on the parts of both of you, thank you!

    As an aside, would you mind explaining how that replace function works?

  8. #8
    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: Regex Pattern Matching

    Hello visualnotsobasic,

    The RegEx replace method, replaces the old text with the new text. A shorthand for referencing a submatch is to use the dollar sign prefix in front of submatch's position. Thus, "$1"indicates the first submatch in the pattern, the four letters. This is concatenated with the second submatch, the numbers, after the numbers are converted from a string to a value and back to string with the required number of zeroes added in.

+ 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] Use inputbox and check regex pattern
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-30-2018, 01:44 PM
  2. RegEx - Pattern Matching
    By paularthur90 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-10-2017, 09:16 AM
  3. Need to Define RegEx pattern
    By hmr2662 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-09-2015, 09:24 AM
  4. RegEx pattern
    By capson in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 02-15-2015, 06:57 PM
  5. Regex: Matching pattern to split a cell data
    By DavidRoger in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 09-01-2014, 07:31 AM
  6. Regex - establishing a pattern
    By twckfa16 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-30-2014, 11:06 AM
  7. Need a new regex .pattern for comparison code
    By Swiss Cheese in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-06-2012, 09:16 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