Pages

Wednesday, 19 October 2011

QTP Script: Executing a regular expression to find text within a string


MsgBox RegularExpExample("QTP.""QTP1 QTP2 qtp3 QTP4")

' =============================================================
' 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(strPatternstrString)

Dim objRegExstrMatchstrMatches 
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 so 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


0 comments:

Post a Comment