Tuesday, May 19, 2009

VBScript Regular Expression Object (RegExp)

The regular expression object depicts a pattern of characters.

Here we will talk about 3 Properties (Global Property, IgnoreCase property and Pattern Property) and 3 Methods (Execute Method, Replace Method and Test Method) of Regular Expression object.

Global Property
Below is an example of Global property of VBScript Regular Expression object. Set the Global to True and it matches the whole string and shows all the occurrences of patterns in the string. Set the Global property to False and as soon as the pattern matches once, it stops the search. Default is False


Function regexp_function(ptrn, strng)
Dim regexp, Match, Matches
'declaring Regular expression Object
Set regexp = New RegExp
'Setting pattern
regexp.Pattern = ptrn
regexp.IgnoreCase = True
regexp.Global = True
Set Matches = regexp.Execute(strng)

For Each Match in Matches
result = result & "Match is found at position "
result = result & Match.FirstIndex & vbCRLF
Next

regexp_function = result
End Function

Msgbox(regexp_function("is", "mississippi"))

This above code when run shows the below message box.



If Global Property is set to False then the result will be




IgnoreCase property
Sets or returns a Boolean value that indicates if a pattern search is case-sensitive or not. The value of the IgnoreCase property is False if the search is case-sensitive, True if it is not. Default is False.

Function regexp_function(ptrn, strng)
Dim regexp, Match, Matches
'declaring Regular expression Object
Set regexp = New RegExp
'Setting pattern
regexp.Pattern = ptrn
regexp.IgnoreCase = True
regexp.Global = True
Set Matches = regexp.Execute(strng)

For Each Match in Matches
result = result & "Match is found at position "
result = result & Match.FirstIndex & vbCRLF
Next

regexp_function = result
End Function

Msgbox(regexp_function("is", "missISsippi"))

For above code where IgnoreCase is true it shows



If IgnoreCase is False then it shows




Pattern Property
Once you create an object, next you assign the regular expression you want to search for to the Pattern property (see lines 3rd and 4th in the above code example). If you want to use a literal regular expression and not the one supplied by user, put the regular expression in a double-quoted string. By default, the regular expression is case sensitive.


Execute Method
The Execute method also takes one string parameter and returns a Matches collection containing a Match object for each match found in string.

If the regex could not match the subject string at all, MatchCollection.Count will be zero.

If the RegExp.Global property is False (the default), MatchCollection will contain only the first match.

If RegExp.Global is true, MatchCollection will contain all matches.

You can see the examples of Global Property and IgnoreCase Property to get a feel of Execute method.


Replace Method
Its syntax is

Object.Replace(string1, string2)

String1 is the text string in which the text replacement is to occur.
String2 is the replacement text string.

Function ReplaceTest(ptrn, replStr)
Dim regexp, str
str = "mississippi"
Set regexp = New RegExp
regexp.Pattern = ptrn
regexp.IgnoreCase = True
regexp.Global=True
ReplaceTest = regexp.Replace(str, replStr)
End Function

MsgBox(ReplaceTest("is", "x"))
'the above function call replaces "is" with "x"

The above code shows




Test Method
Test Method carries out a regular expression search against a specified string and returns a Boolean value that indicates if a pattern match was found.

Function regexp_function(ptrn, strng)
Dim regexp, Match, Matches ,strMatch

Set regexp = New RegExp

regexp.Pattern = ptrn
regexp.IgnoreCase = True
regexp.Global = True
strMatch = regexp.test(strng)
If strMatch Then
regexp_function= "Match Found"
Else
regexp_function= "Match NOT Found"
End If
End Function

Msgbox(regexp_function("is", "mississippi"))

The result of the above code is:




Another simple example to understand Regular Expression Object on Flight Reservation sample application.

Also See:

Regular Expressions in QTP

If you want to learn more on Regular Expression Objects see below:

w3schools
regular-expressions.info
MSDN
4guysfromrolla
wsabstract
mozilla
virtualsplat
microsoftluder
ackind