Working with Regular Expression Object in UFT /QTP. – User friendly Tech help

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.

n

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

n
napplication (AUT).
n
nSolution:-
n

Let’s break the code into smaller steps:-

n

Step1:- Creating regular expression object

n

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

n

Method2:- Set objRegExp = New RegExp

n

Step2:-Using the above object to set the Search pattern

n

objRegExp.Pattern = “Test Pattern”

n

Step3:- Ignore Case insensitivity (By default it is enabled)

n

objRegExp.IgnoreCase = False

n

Step4:- To check pattern should match all occurrences in an entire search string or 
njust the first one.( (By default it is True)

n

 objRegExp..Global = True

n

Note:- Regular expression object has Pattern,IgnoreCase and Global property.

n

Step5:-We will use the Test method, which returns Boolean value, based on the
npattern match in the given string.

n

objRegExp.Test(v_String)

n

Note:- Regular expression has Test, Execute and Replace methods.

n

Code:

n

'*******************************************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")
Was this article helpful?
YesNo

Similar Posts