Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 161
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 161
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 161
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 161
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 161
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regex Pattern Geoff K Excel Programming 5 June 12th 09 12:49 AM
Regex as replace function Excel Monkey[_2_] Excel Programming 3 May 7th 09 06:46 PM
Help with a regex pattern please [email protected] Excel Programming 3 March 22nd 08 12:14 AM
Help with a Regex Pattern [email protected] Excel Programming 11 April 30th 07 01:49 AM
Regex techniques Dave Runyan Excel Programming 5 April 28th 07 12:17 AM


All times are GMT +1. The time now is 06:39 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"