VBScript Scripting Techniques > Regular Expressions
Nowadays, regular expressions are a
powerfull part of most programming and scripting languages.
They allow us to search strings or blocks of text using patterns
instead of just fixed "filter strings".
In VBScript, Regular Expressions use the RegExp object,
which was introduced in Windows Script Host version 5:
Set objRE = New RegExp
Three properties are available for the RegExp
object:
GlobalIgnoreCasePatternThree methods are available for the RegExp
object:
Test( teststring )Execute( teststring )Count.Global = False)ItemFirstIndexLengthSubMatchesCountItemValue
Replace( teststring ).Global = False) or
all (.Global = True) match(es)
replaced by a new string or patternIn general, patterns for WSH's RegExp object are like patterns in any
scripting language.
Special characters and backreferences may differ from other
languages, though.
Use the MSDN list of special characters for WSH's RegExp object.
In this example, all we want to know is wether a string is a valid e-mail
address or not.
So we can use the RegExp object's Test method.
strEmail = "rob.van.der.woude@nodomainofmine.co.uk"
Set objRE = New RegExp
With objRE
.Pattern = "ˆ([\w-]+\.)*[\w-]+@([\w-]+\.)+[a-z]{2,4}$"
.IgnoreCase = True
.Global = False
End With
' Test method returns TRUE if a match is found
If objRE.Test( strEmail ) Then
WScript.Echo strEmail & " is a valid e-mail address"
Else
WScript.Echo strEmail & " is NOT a valid e-mail address"
End If
Set objRE = Nothing
Now that we know that we have a valid e-mail address, let's
extract the domain name.
We will now use the RegExp object's Execute
method, because we want the matching string itself.
strEmail = "rob.van.der.woude@nodomainofmine.co.uk"
Set objRE = New RegExp
With objRE
.Pattern = "ˆ([\w-]+\.)*[\w-]+@(([\w-]+\.)+[a-z]{2,4})$"
.IgnoreCase = True
.Global = False
End With
Set objMatch = objRE.Execute( strEmail )
' We should get only 1 match since the Global property is FALSE
If objMatch.Count = 1 Then
' Item(0) is the (first and only) matching e-mail address,
' Submatches(1) is the substring between the second set of
' parentheses (all indexes are zero based)
WScript.Echo "The domain name for " & strEmail _
& " is """ & objMatch.Item(0).Submatches(1) & """."
Else
WScript.Echo "No domain name was found for """ & strEmail & """."
End If
Set objMatch = Nothing
Set objRE = Nothing
Let's combine what we got so far, and add some code to change the
domain name from "nodomainofmine.co.uk" to
"myowndomain.co.uk".
This time we'll use the Replace method.
strEmail = "rob.van.der.woude@nodomainofmine.co.uk"
Set objRE = New RegExp
With objRE
.Pattern = "ˆ([\w-]+\.)*[\w-]+@([\w-]+\.)+[a-z]{2,4}$"
.IgnoreCase = True
.Global = False
End With
' Test method returns TRUE if a match is found
If objRE.Test( strEmail ) Then
WScript.Echo """" & strEmail & """ is a valid e-mail address."
Else
WScript.Echo """" & strEmail & """ is NOT a valid e-mail address."
Set objRE = Nothing
WScript.Quit 1
End If
objRE.Pattern = "ˆ([\w-]+\.)*[\w-]+@([\w-]+)(\.[\w-]+)*\.[a-z]{2,4}$"
Set objMatch = objRE.Execute( strEmail )
' We should get only 1 match since the Global property is FALSE
If objMatch.Count = 1 Then
' Item(0) is the (first and only) matching e-mail address,
' Submatches(1) is the substring between the second set of
' parentheses (all indexes are zero based)
strDomain = objMatch.Item(0).Submatches(1)
WScript.Echo "The string to be replaced is """ & strDomain & """."
Else
WScript.Echo "No domain name was found for """ & strEmail & """."
Set objRE = Nothing
WScript.Quit 1
End If
Set objMatch = Nothing
objRE.Pattern = "@" & strDomain
strNewDomain = "myowndomain"
strNewMail = objRE.Replace( strEmail, "@" & strNewDomain )
WScript.Echo "The new e-mail address is """ & strNewMail & """."
Set objRE = Nothing
Yes, I know, in real life we wouldn't replace the name like this, it would have been much easier and safer to use
strNewMail = Replace( strEmail, "@" & strDomain, "@" & strNewDomain )
instead.