Rob van der Woude's Scripting Pages

VBScript Scripting Techniques > Regular Expressions

Regular Expressions in VBScript

Nowadays, regular expressions are a powerful 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".

The RegExp object

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:

Three methods are available for the RegExp object:

Patterns

In general, patterns for WSH's RegExp object are like regex 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.

Some Examples

Test if a string is a valid e-mail address:

In this example, all we want to know is whether 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

Extract the domain name from an e-mail address:

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

Change the domain name in an e-mail address

(but not the top level domain):

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.

More Samples


page last modified: 2016-09-19; loaded in 0.0020 seconds