Thread: regEx replace
View Single Post
  #20   Report Post  
Posted to microsoft.public.excel.programming
Claus Busch Claus Busch is offline
external usenet poster
 
Posts: 3,872
Default regEx replace

Hi Robert,

Am Fri, 3 Apr 2015 01:27:59 -0700 schrieb Robert Crandal:

That one didn't work. The output was
"Call me at *"


the pattern is handled as ONE word. So you get only 1 *.
Calculate the length of the substring to replace first:

Sub ReplaceGlobal()
Dim str As String
Dim ptrn As String, re As Object
Dim lLen As Long

str = "Call me at (982)555-1234"
ptrn = "\(\d{3}\)\d{3}-\d{4}"
lLen = Len(str) - InStr(str, "(") - 1

Set re = New RegExp
re.Pattern = ptrn
re.IgnoreCase = False
re.Global = True

MsgBox re.Replace(str, Application.Rept("*", lLen))
End Sub


Regards
Claus B.
--
Vista Ultimate / Windows7
Office 2007 Ultimate / 2010 Professional