Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

RegExp Object for regular expression using VBScript in QTP

VBScript provides a RegExp object to handle regular expressions.This object allows you to find regular expression matches in strings, and replace regex matches in strings with other strings.

Properties of RegExp Object

Following are the properties of regexp object:

IgnoreCase:  By default regular expression is case sensitive, to make it case insensitive, set value as “True”

Pattern: We can define the pattern of regular expression in the pattern property. Pattern can include literal and meta characters like .,*,/d as discussed in the earlier post on how to create regular expressions in QTP

Global: To return or replace all matches, set Global as True. If set as False, finds the first match only


‘’Defining a regular expression
Set newRegExp = New RegExp
newRegExp.IgnoreCase = True
newRegExp.Global = True
newRegExp.Pattern = ".*an"

Methods of RegExp object


Execute method: executes a match against the specified string. Returns a Matchescollection, which contains a Match object for each match. The Matchobject can also contain SubMatches collection. 

Execute method has following properties:

o    Item

       §  Item.value – value of item in the collection
       §  Firstindex – First instance of match location

o    Count

Count of instances matching the regular expression

Replace method: replaces the part of the string found in a match with another string.Does replace the pattern defined for object with the replace value.                                          Syntax: regExpobj.replace(teststring,”newpattern”)

Test method: executes an attempted match and returns True or False based on match found.


Sample Code explaining properties and methods of RegExp Object.


Set newRegExp = New RegExp
newRegExp.IgnoreCase = True
newRegExp.Global = True
newRegExp.Pattern = ".*an"
testString = "this ant is an insect"
Set colmatch = newRegExp.Execute(teststring)
For each match in colmatch
         msgbox match.Value
        msgbox colmatch.count
Next
boolMatch = newRegExp.Test(teststring)
msgbox boolMatch
newRegExp.pattern ="a"
strString = newRegExp.replace(teststring,"b")
msgbox strstring


To understand more on how to use regular expressions in qtp, click here


No comments:

Post a Comment