+ Reply to Thread
Results 1 to 6 of 6

Regex Pattern

  1. #1
    Registered User
    Join Date
    10-04-2012
    Location
    Abbotsford, BC
    MS-Off Ver
    Excel 2010
    Posts
    5

    Regex Pattern

    I have the following text in cell A1 and am trying to replace the -- (double dash) that appears before the 5 digits with a backslash.

    My text in A1 looks like this: MD03_Cncr_Elev3_iv---172--00001.mp3

    After replacing the -- it should look like this

    MD03_Cncr_Elev3_iv---172\00001.mp3

    The pattern will always be -- followed by five digits and a period.

    This is the code I am using but the replace part is not correct as it replaces the entire pattern with a backslash.

    Sub ReplaceDoubleDash()
    Dim regex As Object
    Dim myCell As Range
    Set regex = CreateObject("VBScript.RegExp")
    On Error Resume Next

    regex.Global = True
    regex.Pattern = "[-][-][0-9]{1,5}[.]"
    For Each myCell In Selection.Cells
    myCell.Value = regex.Replace(myCell.Value, "\")
    Next

    End Sub

    Any help would be appreciated.
    Last edited by orange13; 06-07-2022 at 09:22 AM.

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,017

    Re: Regex Pattern

    Until some Regex Expert comes along, try simple VBA:

    Please Login or Register  to view this content.
    As a side note, trying to nest the three Replace functions resulted in the code being blocked as a SQL injection attempt....
    Last edited by Bernie Deitrick; 06-06-2022 at 05:15 PM.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,562

    Re: Regex Pattern

    With a loop maybe.
    Please Login or Register  to view this content.

  4. #4
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,562

    Re: Regex Pattern

    This is what you had in mind Bernie?

    Because "Securi" would not let me publish the code either, it is in the attached text file.
    Attached Files Attached Files

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

    Re: Regex Pattern

    Change the pattern to
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    10-04-2012
    Location
    Abbotsford, BC
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Regex Pattern

    Thank you everyone. I ended up using the one from jindon as after some testing some of the text had more than 3 characters after the period.

+ 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] Regex Pattern Matching
    By visualnotsobasic in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-16-2018, 09:56 PM
  2. [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
  3. RegEx - Pattern Matching
    By paularthur90 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-10-2017, 09:16 AM
  4. Need to Define RegEx pattern
    By hmr2662 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-09-2015, 09:24 AM
  5. RegEx pattern
    By capson in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 02-15-2015, 06:57 PM
  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