Sunday, October 15, 2017

Regular Expression


*******************************************************************************
str="this day this week this month this year"
pat="this"
patternFountAt str,pat

Function patternMatches(str,pat)
Set regex= new RegExp 'Set is the collection ,RegExp-is the object we are intiating

'Properties of RegExp

regex.Global=true        
regex.ignorecase=true    
regex.pattern=pat

'Methods of RegExp
print "Pattern found "&regex.test(str)
Set matches=regex.execute(str)
print matches.count


For i = 0 To matches.count-1 Step 1

Set match=matches(i)
print "the first match at index "& match.firstindex &".match values is "& match.value
Next

End Function
******************************************************************************** 

###############################################################################
str="this day this week this month this year"
pat="this"

'**************************************************************************
'Regular expression object
'RegExp-OBJECT

Set regex= new RegExp 'Set is the collection ,RegExp-is the object we are intiating

'PROPERTIES

        '-Global-the pattern should be found at every place in string
        '-ignorecase-'ignore the case while finding pattern
        '-Pattern-used to find the pattern from the string
    
regex.Global=true        
regex.ignorecase=true    
regex.pattern=pat
'************************************************************
'METHODS

'test-is the method to test whether pattern is found in the string
'execute
print "Pattern found "&regex.test(str)

'matches- is collection,it holds all matching pattern from the string

Set matches=regex.execute(str)
print matches.count


'match-is the oject
Set match=matches(0)
print match.firstindex
print match.value


Set match=matches(1)
print match.firstindex
print match.value

'match object

For i = 1 To matches.count-1 Step 1
Set match=matches(0)
print "the first match at index "& match.firstindex &".match values is:= "& match.value
Next

####################################################################################
 %%%%%%EXAMPLES*************

s="then this there they them acthbn"
p="\bth"'the letter that starts with "th" in the string()
'Pattern found True
'5
'the  match founf at index -0.match values is :-th
'the  match founf at index -5.match values is :-th
'the  match founf at index -10.match values is :-th
'the  match founf at index -16.match values is :-th
'the  match founf at index -21.match values is :-th
'-----------------------------------------------------
p="th.*"

s="then this there they them acthbn"' the letters 'th' that is anywhere in the string output :-
'1
'the  match founf at index -0.match values is :-then this there they them acthbn
'---------------------------------------------------------------------------
p="\bth+"

s="then this there they them acthbn"
'Pattern found True
'5
'the  match founf at index -0.match values is :-th
'the  match founf at index -5.match values is :-th
'the  match founf at index -10.match values is :-th
'the  match founf at index -16.match values is :-th
'the  match founf at index -21.match values is :-th
'----------------------------------------------------------------
p="th+"

s="then this there they them acthbn"
'Pattern found True
'6
'the  match founf at index -0.match values is :-th
'the  match founf at index -5.match values is :-th
'the  match founf at index -10.match values is :-th
'the  match founf at index -16.match values is :-th
'the  match founf at index -21.match values is :-th
'the  match founf at index -28.match values is :-th
'----------------------------------------------------------
p="th{1,}"

s="then this there they them acthbn"
'Pattern found True
'6
'the  match founf at index -0.match values is :-th
'the  match founf at index -5.match values is :-th
'the  match founf at index -10.match values is :-th
'the  match founf at index -16.match values is :-th
'the  match founf at index -21.match values is :-th
'the  match founf at index -28.match values is :-th
'
'---------------------------------------------------------------------
s="603 2334 777"
p="[0-9]"
'Pattern found True
'10
'the  match founf at index -0.match values is :-6
'the  match founf at index -1.match values is :-0
'the  match founf at index -2.match values is :-3
'the  match founf at index -4.match values is :-2
'the  match founf at index -5.match values is :-3
'the  match founf at index -6.match values is :-3
'the  match founf at index -7.match values is :-4
'the  match founf at index -9.match values is :-7
'the  match founf at index -10.match values is :-7
'the  match founf at index -11.match values is :-7
'--------------------------------------------------------------------------

s="603 2334 777"
p="[0-9]{1,}"
'Pattern found True
'3
'the  match founf at index -0.match values is :-603
'the  match founf at index -4.match values is :-2334
'the  match founf at index -9.match values is :-777
'--------------------------------------------------
s="603 2334 777"
p="[0-9]{3}"
'3
'the  match founf at index -0.match values is :-603
'the  match founf at index -4.match values is :-233
'the  match founf at index -9.match values is :-777
'patternMatches s,p
'-------------------------------------------------
s="the completion of selection process"
P="[a-z]+tion\b"
'Pattern found False
'0
'P
'
patternMatches s,p
'-----------------------------------------------
s="the completion of selection process"
P="completion|selection"
'Pattern found True
'2
'the  match founf at index -4.match values is :-completion
'the  match founf at index -18.match values is :-selection
'-----------------------------------------------------------
s="Welcome sudheer.you have 7777 unread mails"
p="[a-zA-Z0-9]+"
'Pattern found True
'7
'the  match founf at index -0.match values is :-Welcome
'the  match founf at index -8.match values is :-sudheer
'the  match founf at index -16.match values is :-you
'the  match founf at index -20.match values is :-have
'the  match founf at index -25.match values is :-7777
'the  match founf at index -30.match values is :-unread
'the  match founf at index -37.match values is :-mails
'---------------------------------------------------------
s="HGF54-GGGG-SDFGR-QWERT-CVBDS-OIUIU"
p="[A-Z0-9]{5}-{0,1}"
'{0,1}"{lower,upper}
'Pattern found True
'5
'the  match founf at index -0.match values is :-HGF54-
'the  match founf at index -11.match values is :-SDFGR-
'the  match founf at index -17.match values is :-QWERT-
'the  match founf at index -23.match values is :-CVBDS-
'the  match founf at index -29.match values is :-OIUIU
'-------------------------------------------------------------------------------------
s="Email id is sudheer@gmail.com or sud3eer@gmail.com or sudheerqa.selenium@gmail.com"
p="[A-Za-z0-9.]{3,}@[a-zA-Z]{1,}[.][a-z-A-Z]{1,}"
'Pattern found True
'3
'the  match founf at index -12.match values is :-sudheer@gmail.com
'the  match founf at index -33.match values is :-sud3eer@gmail.com
'the  match founf at index -54.match values is :-sudheerqa.selenium@gmail.com
'------------------------------------------------------------------------------------------------
s="dates are  21/01/2014 09/12/2015 31/02/2017 02/08/2016"
p="((0[1-9])|(1[0-9])|(2[0-9])|(3[0-9]))/((0[1-9])|(1[0-2]))/[0-9]{4}"
'Pattern found True
'4
'the  match founf at index -11.match values is :-21/01/2014
'the  match founf at index -22.match values is :-09/12/2015
'the  match founf at index -33.match values is :-31/02/2017
'the  match founf at index -44.match values is :-02/08/2016
'-------------------------------------------------------------------------
patternMatches s,p

Function patternMatches(str,pat)
Set regex= new RegExp 'Set is the collection ,RegExp-is the object we are intiating

'Properties of RegExp

regex.Global=true           
regex.ignorecase=true       
regex.pattern=pat

'Methods of RegExp
print "Pattern found "&regex.test(str)
Set matches=regex.execute(str)
print matches.count


For i = 0 To matches.count-1 Step 1

Set match=matches(i)
print "the  match founf at index -"& match.firstindex &".match values is :-"& match.value
Next
   
End Function
 

No comments:

Post a Comment

Description Programming

'==Descriptive Programming language 'creating the common browser and page object  Set  myPage= browser( "creationtime:=0...