Recent Post

QTP - Regular Expressions - Scripts

QTP – Regular Expressions

'RegExp
 Method
Dim strRet
' create regular expression object
Set objRegEx = New RegExp
' set the pattern
objRegEx.Pattern = strPattern
' set it be not case sensitive
objRegEx.IgnoreCase = True
' set global flag =true sothat we search all of the string, instead of just searching  for the first occurrence
objRegEx.Global = True
' execute search
Set strMatches = objRegEx.Execute(strString)
'=====================================================

'Execute Method
'####################################################
' @ Executing a regular expression to find text within a string.
'####################################################
MsgBox RegularExpExample("QTP.", "QuickTestQTP, by GAReddy and QuickTestQTP is qtp's blog")
' =============================================================
' @Function: RegularExpExample
' @Desc :    Example of how to use the regular expression object to find text within a string
' @Params :  strPattern is the regular expression
'            strString is the string to use the expression on
' @Returns : An example string showing the results of the search
' =============================================================
Function RegularExpExample(strPattern, strString)
Dim objRegEx, strMatch, strMatches
Dim strRet
' create regular expression object
Set objRegEx = New RegExp
' set the pattern
objRegEx.Pattern = strPattern
' set it be not case sensitive
objRegEx.IgnoreCase = True
' set global flag =true sothat we search all of the string, instead of just searching  for the first occurrence
objRegEx.Global = True
' execute search
Set strMatches = objRegEx.Execute(strString)
' for each match
For Each strMatch in strMatches
   strRet = strRet & "Match found at position '" & _
            strMatch.FirstIndex & "' - Matched Value is '" & _
            strMatch.Value & "'" & vbCRLF
Next
RegularExpExample = strRet
End Function ' RegularExpExample

'Test Method
'########################################################################
  '@Using Locate to determine if specific text exists within a string.
 '#######################################################################
MsgBox LocateText("www.QuickTestQTP.blogpsot.com", "QTP")
MsgBox LocateText("www.QuickTestQTP.blogpsot.com", "QTP.*.com")
' =============================================================
' @Function: LocateText
' @Desc :    Uses a regular expression to locate text within a string
' @Params :  strString is the string to perform the search on
'            strPattern is the regular expression
' @Returns : True if the pattern was found, False otherwise
' =============================================================
Function LocateText(strString, strPattern)
Dim objRegEx
' create the regular expression
Set objRegEx = New RegExp
' set the pattern
objRegEx.Pattern = strPattern
' ignore the casing
objRegEx.IgnoreCase = True
' perform the search
LocateText = objRegEx.Test(strString)
' destroy the object
Set objRegEx = Nothing
End Function ' LocateText

'Replace Method
'#################################################################
  '@Using the replace method to find and replace text in a string.
 '#################################################################
MsgBox ReplaceText("QuickTest QTP: Learning QTP is easy .", "easy.", "easiest")
MsgBox ReplaceText("QTP seems to be  easy .Just look here, everything else about QTP exists: but implemenation is HARD...!","but.*","!")
' =============================================================
' @Function: ReplaceText
' @Desc :    Uses a regular expression to replace text within a string
' @Params :  strString is the string to perform the replacement on
'            strPattern is the regular expression
'            strReplacement is the replacement string
' @Returns : ReplaceText
' =============================================================
Function ReplaceText(strString, strPattern, strReplacement)
Dim objRegEx
' create the regular expression
Set objRegEx = New RegExp
' set the pattern
objRegEx.Pattern = strPattern
' ignore the casing
objRegEx.IgnoreCase = True
' make the replacement
ReplaceText = objRegEx.Replace(strString, strReplacement)
' destroy the object
Set objRegEx = Nothing
End Function ' ReplaceText

0 comments:

Post a Comment

GAReddy @ OneTestingCenter @ All Articles