February 15, 2012

Using Callbacks on the Regexp.Replace function

On our testprojects, I use regular expressions most of the time for the matching or testing of strings on our Application Under Test. But sometimes I have to use the Replace method of the RegExp object. This day, I discovered an advanced usage of the RegExp.Replace() method. Let me show you with an example:

Dim replacement
Dim regEx : Set regEx = New RegExp ' Create a regular expression.
Dim myString : myString = "Cake, and grief COUNSELING, will be Available at the CoNcLuSiOn of the test."

regEx.Pattern = "\b\w+\b" ' \b = inside word boundary, \w = word character,
                          ' so this pattern matches each single word
regEx.Global = True       ' Set the scope to Global

' normally we replace like this:
replacement = "word"
MsgBox regEx.Replace(mystring, replacement)

' resulting in: "word, word word word, word word word word word word word word word."

This looks familiar I think. But it is not very sophisticated. Wouldn't it be nice if we can replace each match with a custom replacement. With the use of a Function Reference, we can!

 ' We can actually perform a custom action on replace instead of doing a fixed replace
' We do this with making a reference to a function with getref
Set replacement = getRef("Capitelize")

' We need to create a function of course with the same name and three parameters
' 1. singleMatch : the string that matched to the pattern
' 2. position : the position the singlematch string was found
' 3. fullString : the full string (same as 'myString' in this case) without _any_ replacements
Function Capitelize(singleMatch, position, fullString)

    ' Capitelize returns the string singleMatch changed with the first character
    ' in uppercase and all others in lowercase
    Capitelize = ucase(left(singleMatch,1)) & lCase(mid(singleMatch,2))
End Function

MsgBox regEx.Replace(myString, replacement)
' This results in: "Cake, And Grief Counseling, Will Be Available At The Conclusion Of The Test."

Nice huh? Now we can match for a date and replace it on the fly by the correct format. We can replace straight quote characters for curly quotes with the correct orientation with much more ease, or only do a replace if it is within a certain character range in the string. "All new possibilities arise!"

32 comments:

Vinoth said...

This is awesome.
But i am wondering how it works fine een if we do not pass the arguments singleMatch, position, fullString explicitly.

Could you please xplain

veneer pressing machine said...

I am totally agree with the @vinoth..
even i am also wandered for the same...
:D

Vinod Kumbhar said...


ABB PLC, Allen Bradley, CX One, Delta, Free PLC Software Download, GE Poficy HMI, GE VersaMax, GX Developer, LOGO, Mitsubishi, Omron, Pico, RSLogix5000, Schneider Twido, Siemens PLC, Step7 MicroWin,

PLC SCADA Ebook ,Softwares

Gopi said...

Good One!! I googled for it and got you article. Its really helped me a lot and you saved my time. Really its working good. Thanks a lot man!!
If your audience is also interested in Selenium Testing, they can take a look here: Selenium Training

Anonymous said...

This works realy great.! Thank you!

sasitamil said...


After reading this web site I am very satisfied simply because this site is providing comprehensive knowledge for you to audience.
Thank you to the perform as well as discuss anything incredibly important in my opinion. We loose time waiting for your next article writing in addition to I beg one to get back to pay a visit to our website in


Selenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training

service care said...

I am obliged to you for sharing this piece of information here and updating us with your resourceful guidance. Hope this might benefit many learners. Keep sharing this gainful articles and continue updating for us.
motorola service center near me
motorola mobile service centre in chennai
moto g service center in chennai

Realtime Experts said...

This is amazing and really inspiring goal.citrix training in bangalore


Realtime Experts said...


thank you so much for this nice information Article, Digitahanks for sharing your post with us.WEBMETHODS training in bangalore

Realtime Experts said...

This is amazing and really inspiring goal.Hadoop Training in Bangalore

Realtime Experts said...


I have read your blog its very attractive and impressive. I like it your blog.Informatica Training in Bangalore

Anonymous said...

I get a lot of great information from your blog. Thank you for your sharing this informative blog. I have bookmarked this page for my future reference.



Big Data Hadoop Training In Chennai | Big Data Hadoop Training In anna nagar | Big Data Hadoop Training In omr | Big Data Hadoop Training In porur | Big Data Hadoop Training In tambaram | Big Data Hadoop Training In velachery

deiva said...

Good One!! I googled for it and got you article. Its really helped me a lot and you saved my time. Really its working good. Thanks a lot man!!
java training in chennai

java training in omr

aws training in chennai

aws training in omr

python training in chennai

python training in omr

selenium training in chennai

selenium training in omr

Jayalakshmi said...

This post is really a fastidious one it assists new web people, who are wishing for blogging.
data science training in chennai

data science training in tambaram

android training in chennai

android training in tambaram

devops training in chennai

devops training in tambaram

artificial intelligence training in chennai

artificial intelligence training in tambaram

praveen said...

You have shared useful and important script with us. Keep updating.

sap training in chennai

sap training in porur

azure training in chennai

azure training in porur

cyber security course in chennai

cyber security course in porur

ethical hacking course in chennai

ethical hacking course in porur

radhika said...

You have shared useful and important script with us. Keep updating.

AWS training in Chennai

AWS Online Training in Chennai

AWS training in Bangalore

AWS training in Hyderabad

AWS training in Coimbatore

AWS training

jeni said...

I am glad it is helpful. Hopefully I will be able to release a newer version this month with some updates and improvements.
java training in chennai

java training in velachery

aws training in chennai

aws training in velachery

python training in chennai

python training in velachery

selenium training in chennai

selenium training in velachery

shiny said...

Your articles really impressed for me,because all information are so nice. keep posting.

hardware and networking training in chennai

hardware and networking training in annanagar

xamarin training in chennai

xamarin training in annanagar

ios training in chennai

ios training in annanagar

iot training in chennai

iot training in annanagar

vivekvedha said...

I get a lot of great information from your blog. Thank you for your sharing this informative blog.
acte reviews

acte velachery reviews

acte tambaram reviews

acte anna nagar reviews

acte porur reviews

acte omr reviews

acte chennai reviews

acte student reviews

Data Science said...

Awesome article with valuable information it very helpful waiting for next blog update thank you.
Data Analytics Course Online 360DigiTMG

Tech Institute said...

Excellent blog with very impressive writing and unique content, information shared was very valuable thank you.
Data Science Course in Hyderabad

Lavanya said...

This is really an awesome post, thanks for it. Keep adding more information to this.
salesforce training in chennai

software testing training in chennai

robotic process automation rpa training in chennai

blockchain training in chennai

devops training in chennai

rajmohan1140 said...

Nice article Man Keep up the good work. And I would like to be here again to find another masterpiece article. Thanks for sharing.

Java Training in Chennai

Java Course in Chennai

Machine learning Classes in Pune said...

This is a really very informative article, Thanks for sharing this information.

Anonymous said...


Thanks for the Valuable information.Really useful information. Thank you so much for sharing. It will help everyone.

SASVBA Delhi provides extensive MERN stack training in Delhi. The extensive hands-on session, hosted by the MERN Stack Training Institute in Delhi, includes live projects and simulations.

FOR MORE INFO:

tejaswani blogs said...


Work is fantastic. Thank you very much for sharing this useful blog with us. My problem has been solved.

1000 Social BookMarking Sites List
Free social bookmarkeing sites list

Priya Rathod said...

Thank you for taking the time to publish this information very useful!
Python Training in Hyderabad
Python Course in Hyderabad

Priya Rathod said...

Nice blog. I finally found great post here Very interesting to read this article and very pleased to find this site. Great work!
Data Science Training in Hyderabad
Data Science Course in Hyderabad

traininginstitute said...

Great tips and very easy to understand. This will definitely be very useful for me when I get a chance to start my blog.
data scientist course

Data Science said...

This post is very simple to read and appreciate without leaving any details out. Great work!
360DigiTMG data analytics course

vrindha said...

Ransomware-type viruses can badly affect your system and the files contained in it. These viruses first steal data like login details, browsing history, IP address, and cookies. After this, they start damaging hardware parts and software that are already installed in the system. xhamster ransomware

Anonymous said...

Great post, keep us posted valuable information Software Testing Training in Pune