Working with Regular Expression Object in UFT /QTP.

Today we are going to touch the key concept in Regular Expression i.e “Creation
of  Regular Expression object” and using this we’ll search a given pattern in a string.
Regular expression is a frequently used concept in Automation WORLD.

Scenario:- To find the existence of a pattern inside a string which is present in Your 

application (AUT).

Solution:-

Let’s break the code into smaller steps:-

Step1:- Creating regular expression object

Method1:- Set objRegExp = CreateObject(“VBScript.RegExp”)
Method2:- Set objRegExp = New RegExp

Step2:-Using the above object to set the Search pattern
objRegExp.Pattern = “Test Pattern”

Step3:- Ignore Case insensitivity (By default it is enabled)
objRegExp.IgnoreCase = False

Step4:- To check pattern should match all occurrences in an entire search string or 
just the first one.( (By default it is True)
 objRegExp..Global = True
Note:- Regular expression object has Pattern,IgnoreCase and Global property.

Step5:-We will use the Test method, which returns Boolean value, based on the
pattern match in the given string.
objRegExp.Test(v_String)
Note:- Regular expression has Test, Execute and Replace methods.

Code:

'*******************************************Sub***************************************************
'Function Name:- fnPatternMatch
'Function Description:- Used to check the particular pattern inside
a given string

'Input Parameters:- Pattern to be searched , String
'Output Parameters:- None
'Created By :- www.uftHelp.com
'Date:- 17th Feb,2014
'****************************************************************************************************

Function fnPatternMatch(v_String,v_Pattern)
' Create variable.
Dim objRegExp

' Create regular expression object
Set objRegExp = New RegExp
'Set objRegExp = CreateObject("VBScript.RegExp")
With objRegExp
.Pattern = v_Pattern ' Set pattern.
.IgnoreCase = False ' Set case insensitivity.
.Global = True ' Set global applicability.
End with
' Test the occurence of pattern inside the string
v_Flag = objRegExp.Test(v_String)
'Check whether pattern is found
If v_Flag Then
fnPatternMatch = "Match is Found"
Else
fnPatternMatch = "Sorry! No match found"
End If
Set objRegExp = Nothing
End Function

msgbox fnPatternMatch("uftHelp.com Your first TestingHelp","uftHelp.com")
msgbox fnPatternMatch("uftHelp.com Your first TestingHelp","uftHelp.in")