Home |
Search |
Today's Posts |
|
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
regEx replace
If a substring is found that matches my regular expression
pattern, I would like each character to be replaced with a "*" character. I thought the following code would work, but I'm missing something? : '-------------------------------------------------------- Sub myregex() Dim regEx As New VBScript_RegExp_55.RegExp Dim str As String str = "Call me at (982)555-1234!" regEx.Pattern = "(...)...-...." regEx.Global = True regEx.IgnoreCase = True MsgBox regEx.Replace(str, "*") End Sub |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
regEx replace
"Robert Crandal" wrote:
'-------------------------------------------------------- Sub myregex() Dim regEx As New VBScript_RegExp_55.RegExp Dim str As String str = "Call me at (982)555-1234!" regEx.Pattern = "(...)...-...." regEx.Global = True regEx.IgnoreCase = True MsgBox regEx.Replace(str, "*") End Sub If I wasn't clear, the output message should be: ************* But, I would also be happy if the output was: (***)***-**** Either way is fine, but the first is better. |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
regEx replace
Hi Robert,
Am Thu, 2 Apr 2015 03:08:31 -0700 schrieb Robert Crandal: If I wasn't clear, the output message should be: ************* But, I would also be happy if the output was: (***)***-**** try: Sub ReplaceGlobal() Dim str As String Dim ptrn As String, re As Object str = "Call me at (982)555-1234!" ptrn = "[0-9]" Set re = New RegExp re.Pattern = ptrn re.IgnoreCase = False re.Global = True MsgBox re.Replace(str, "*") End Sub Regards Claus B. -- Vista Ultimate / Windows7 Office 2007 Ultimate / 2010 Professional |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
regEx replace
Hi again,
Am Thu, 2 Apr 2015 03:08:31 -0700 schrieb Robert Crandal: If I wasn't clear, the output message should be: ************* Sub ReplaceGlobal() Dim str As String Dim ptrn As String, re As Object str = "Call me at (982)555-1234!" ptrn = "\(|\)|-|!|\d" Set re = New RegExp re.Pattern = ptrn re.IgnoreCase = False re.Global = True MsgBox re.Replace(str, "*") End Sub Regards Claus B. -- Vista Ultimate / Windows7 Office 2007 Ultimate / 2010 Professional |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
regEx replace
"Claus Busch" wrote:
Hi again, str = "Call me at (982)555-1234!" ptrn = "\(|\)|-|!|\d" Wow, I never woulda guessed that expression. Can you explain how it works? All I see are a couple of alternation "|" symbols and one digit "\d" metacharater. Also, what pattern works for Social Security numbers? |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
regEx replace
Hi Robert,
Am Thu, 2 Apr 2015 09:47:12 -0700 schrieb Robert Crandal: Can you explain how it works? All I see are a couple of alternation "|" symbols and one digit "\d" metacharater. \d is equivalent to [0-9] To find parentheses you have to put a back slash in front \( and \) But I am not an expert with Regulkar Expressions. I always play around with try and error. Regards Claus B. -- Vista Ultimate / Windows7 Office 2007 Ultimate / 2010 Professional |
#7
Posted to microsoft.public.excel.programming
|
|||
|
|||
regEx replace
"Claus Busch" wrote:
\d is equivalent to [0-9] To find parentheses you have to put a back slash in front \( and \) But I am not an expert with Regulkar Expressions. I always play around with try and error. Yup, I'm not a regular expression expert either. For the pattern, my guess was: "\(\d\d\d\)\d\d\d-\d\d\d\d" The only problem was that the entire string got replaced with a single "*", instead of filling in all 13 characters with a "*". |
#8
Posted to microsoft.public.excel.programming
|
|||
|
|||
regEx replace
Hi again,
Am Thu, 2 Apr 2015 09:47:12 -0700 schrieb Robert Crandal: Can you explain how it works? All I see are a couple of alternation "|" symbols and one digit "\d" metacharater. the alternation sign splits all characters in your string and the characters are searched one by one. If you want to do it with a pattern try: ptrn = "[\(\d\)\d-\d!]" can you post an example of Social Security numbers? I don't know how they look like in your country. Regards Claus B. -- Vista Ultimate / Windows7 Office 2007 Ultimate / 2010 Professional |
#9
Posted to microsoft.public.excel.programming
|
|||
|
|||
regEx replace
"Claus Busch" wrote:
can you post an example of Social Security numbers? I don't know how they look like in your country. In my country, they are typically 123-45-6789. So, my guess for the pattern would be: regEx.Pattern = "\d\d\d-\d\d-\d\d\d\d" |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Regex Pattern | Excel Programming | |||
Regex as replace function | Excel Programming | |||
Help with a regex pattern please | Excel Programming | |||
Help with a Regex Pattern | Excel Programming | |||
Regex techniques | Excel Programming |