Thursday 31 October 2013

Working with Regular Expression (RegExp) Object in QTP

Function 

Function FindPatternMatches(str,pat)
Set regEx = New RegExp
regEx.pattern=pat
regEx.ignorecase=true
regEx.global=true

msgbox "Patterne found-"&regEx.test(str)
Set matches=regEx.execute(str)
msgbox "Total Matchec found-"&matches.count
For i = 0 To matches.count-1
Set match=matches(i)
msgbox "Value found at index-"&match.firstindex&"..Match value---"&match.value
Next

End Function

Examples

s="Welcome Sudhakar you have 88 Unread Mails"
p="[a-zA-Z0-9]+"
p="^Welcome[a-zA-Z]+[.]\s you have[0-9]+Unread Mails[.]"
p="([a-zA-Z]+)([.])|([0-9]+)"
FindPatternMatches s,p


s="HCQ9D-TVCWX-X9QR1-J4B2Y-2GR2T"
p="[A-Z0-9]{5}-{0,1}"
FindPatternMatches s,p


s="Email id is mg_sudha06@yahoo.co.in or mgsudha09@rediffmail.com or sudhakar8871@gmail.com"
p="[a-zA-Z0-9]{3,}@[a-zA-Z]{1,}[.][a-zA-Z]{1,}"
FindPatternMatches s,p


s="dates are 21/01/2012 01/11/2013 01/01/2014 23/01/2014"
p="((0[0-9])|(1[0-9])|(2[0-9])|(3[0-9]))/((0[1-9])|(1[0-2]))/[0-9]{4}"

FindPatternMatches s,p

***********************************************************************************

No comments:

Post a Comment