+ Reply to Thread
Results 1 to 3 of 3

RegExp Pattern to remove special character

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-17-2013
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    132

    RegExp Pattern to remove special character

    Hi,

    I have data in range A1 to A100 with format like AB-CD12-34567890-EF123 and I would like to remove all occurrence of dash (-) except first dash. So the output should be AB-CD1234567890EF123 in Cell B1 and so on.

    I know this can be done using other technique however I am trying to learn Regular Expression so would prefer solution trough same.

    Please help.

    Regards,

  2. #2
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: RegExp Pattern to remove special character

    Here's one approach:
    Function CleanChars(mytext As String) As String
    Dim RE As Object
    Dim REPattern As String
    Dim REMatches As Object
    Dim Bullpen As String
    Dim DashPos As Integer
    
    DashPos = InStr(1, mytext, "-")
    Bullpen = Mid(String:=mytext, Start:=DashPos + 1)
    
    Set RE = CreateObject("vbscript.regexp")
    
    REPattern = "-"
    RE.Global = True
    RE.ignorecase = True
    RE.Pattern = REPattern
    
    CleanChars = Left(mytext, DashPos) & RE.Replace(Bullpen, "")
        
    End Function
    Is that something you can work with?
    Ron
    Former Microsoft MVP - Excel (2006 - 2015)
    Click here to see the Forum Rules

  3. #3
    Forum Contributor
    Join Date
    08-17-2013
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    132

    Re: RegExp Pattern to remove special character

    Thanks Ron, that worked.

    Just for my better understanding, Can this be done using only Replace method (without splitting the string) of Regular Exp and if yes then how to tell replace method to replace a particular position of special character. Let say if i have same string with some more digits and dashes and I would like to remove 1,2,5,6 occurrence of special char(-).

    Regards,

+ 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. remove a special character using VBA
    By phwjr in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-06-2013, 02:33 PM
  2. [SOLVED] Syntax to make long RegExp Pattern readable?
    By ahartman in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-16-2010, 02:25 PM
  3. RegExp Pattern
    By MBCMDR in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-08-2009, 02:09 PM
  4. remove special character at end of string
    By captain bob in forum Excel General
    Replies: 0
    Last Post: 08-03-2006, 09:59 AM
  5. [SOLVED] pattern matching w/o using VBScript's RegExp Object
    By Chris W. in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-07-2005, 06:06 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