site
stats
By Anish Pillai Anish Pillai Posted under Not So Common Stuff | QTP Concepts

RegExp Object: An Alternative to InStr() Function ?

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×

Did you ever encounter a situation where you needed to search a sub-string inside a string? Of course InStr() is a great function which helps users solve this problem. But what happens when the sub-string you want to search contains a regular expression? InStr() doesn’t help when the user wants to search a regular expression. In such situations, RegExp object comes to your rescue. Let’s see how..

 

sString = “Welcome User, You have 5 New Mails”

sSubString = “Welcome .*, You have .* New Mails”‘Check if the string pattern matches – using RegExp object

Set objRE = New RegExp

objRE.Global = True

objRE.Pattern = sSubString

msgbox objRE.Execute(sString).Count ‘ — msgbox returns the value 1 as the RegExp object returns 1 match‘Check if the string pattern matches  – using InStr

msgbox InStr(1, sString, sSubString, 1)  ‘ — msgbox returns the value 0  as it is not able to find the match using InStr

Let’s explore all the features of RegExp object

RegExp object Properties:RegExp object supports the following 3 properties:
a) Global Property: This property indicates whether QTP should match all the occurrences of the pattern in the search string or just the first occurence. The property can take 2 values – True or False. True indicates that the pattern should match all occurrences in the string while False specifies that only the first occurrence of the pattern is matched.
Set regEx = New RegExp
regEx.Pattern = “test”
regEx.Global = True  ‘ — or False                                                                                     
msgbox regEx.Execute(“test testing tested”).Count

In this example, if the value of Global property is set as True, the msgbox count would come as 3. If the Global property value is set as False, the msgbox count comes as 1.

b) IgnoreCase Property: This property specifies whether the pattern should be searched in case-sensitive manner or not. This property takes 2 values – True or False. False indicates that the search is case-sensitive while True specifies otherwise.
Set regEx = New RegExp
regEx.Pattern = “test”

regEx.Global = True

regEx.IgnoreCase = True  ‘ — or False                                                                                     

msgbox regEx.Execute(“Test testing TeSted”).Count

Here, if the IgnoreCase property is taken as True, then the msgbox count will come as 3. For the value False, msgbox returns the count as 1.

c) Pattern Property: This property specifies the actual pattern that needs to be matched with the string. The pattern string can contain regular expressions also. [Read more about regular expression patterns here]

 

RegExp object Methods: RegExp object supports the following methods –
a) Execute Method: This method executes a regular expression search against a specified string and returns a collection of objects based on the search result.
Set regEx = New RegExp
regEx.Pattern = “test”

regEx.Global = True

                

Set objResult = regEx.Execute(“test testing tested”)             

msgbox objResult.Count

b) Replace Method:

Replace method replaces the text found in a regular expression search. The method takes 2 parameters –

object.Replace(string1, string2)
string1 - the string in which the text needs to be replaced
string2 - the replacement string
str1 = “long longer longest”
str2 = “short”
Set regEx = New RegExp
regEx.Pattern = “long”

regEx.Global = True             

sReplace = regEx.Replace(str1, str2)
msgbox sReplace                                                  

In this example, the text ‘long’ is replaced by ‘short’ and the msgbox returns short shorter shortest

c) Test Method: This method executes a regular expression search against a specified string and returns a Boolean value which indicates if a pattern match was found or not. The method returns True if a pattern match is found; False if no match is found.
Set regEx = New RegExp
regEx.Pattern = “test”

regEx.Global = True              

msgbox regEx.Test(“test testing tested”)                         

 

In this example, the msgbox returns the Boolean value True as the pattern match is found.

 

Want to learn more about Regular Expressions in QTP? Check –
this link for various regular expression patterns &
this link on how to use Regular Expression evaluator in QTP 11.
Thanks Joe Colantonio, for providing the idea for this post.

 

If you enjoyed this article, you can join our blog to get free email updates directly in your inbox.

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×
0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×