Regular
Expression can be used to find the text strings and objects with varying values.
·
Regular Expression
will be helpful when the expected value of any object or string is regularly
changing but in fixed pattern.
·
These are strings
which defines the search phrases on the basis of special character provided in
the expression.
·
Scenarios where
Regular Expression can be utilized:
1.
Defining the
property values of an object
2.
Defining the
expected values for checkpoints
3.
Defining pop-up
window conditions in recover scenario
Points
about Regular Expression:
v
It can be created
only for strings only.
v
Period (.), hyphen
(-), asterisk (*), caret (^), brackets ([ ]), parentheses (()), dollar sign
($), vertical line (|), plus sign (+), question mark (?), and backslash (\) are
special characters used to create RE.
v
Any of the above
mentioned special characters is preceded by a backslash (\), QTP treats it as a
literal character.
Various Regular Expression Pattern used
in QTP:
Alpha Numeric Character:
1. Matching Any Alpha Numeric Character Including the
Underscore ( \w )
2. Matching Any Non-Alpha Numeric Character (\W) will match any special character other than underscore.
Please note case of W in this case.
Digit Character:
1. Matches a digit character (\d) matches a digit value.
2. Matching a non-digit character (\D) matches
a non digit valu e
Single
Character:
1.
Matching Any
Single Character (.) e.g. def. Will
match ‘def’ followed by any character.
2.
Matching Any
Single Character in a List ( [xy] ) e.g.
[de] will match either d or e
3.
Matching Any
Single Character Not in a List ( [^xy] ) e.g.
4[^56] will match all values between 41 to 49 except 45 and 46.
4.
Matching Any
Single Character within a Range ( [x-y] ) e.g.
: 2[1-3] will match 21,22, and 23.
Specific Characters:
1.
Matching Zero or
More Specific Characters ( * ) This
matches zero or more occurrences of the preceding character. e.g de* will match
dee, deeee, d and so on. Similarly d.* will match d, ds, deee, and so on, since
preceding character here is “.”.
2.
Matching One or
More Specific Characters ( + ) Only
difference from * is it will match for minimum one character. e.g se+t will
match seat,set but not st as in above case.
3.
Matching Zero or
One Specific Character ( ? ) A
question mark (?) instructs QTP to match zero or one occurrences of the
preceding character. For example: ze?s matches zes and zs, but nothing else
4.
Matching One of
Several Regular Expressions ( | )
e.g date|day will match either of date or day. If we write da(t|e)ay, it will
match datay or daeay.
Line:
1. Matching the Beginning of a Line ( ^ ) This will match only if match is found at beginning
of line.
2. Matching the End of a Line ( $ ) This will match only if match is found at end of
line.
Boundary:
1. Matching a word at boundary(\b) e.g end\b will match testend but not in gendit.
Regular Expression Examples:
There are 3 properties and 3 methods in the Regular
Expression.
Properties:
1. Pattern
2. Global
3. Ignorecase
Methods:
1. Execute(
)
2. Test()
3. Replace()
Properties syntax:
1) Pattern:-
Property is used to store expected
expression for matching.
Syntax:-
set r=new regexp
r.pattern=”\w”
r.global=true/false
2) Global:- Property is used to specify pattern matching on given
text until end of text.
Syntax:-
set r=new regexp
r.pattern=”\d”
r.global=true/false
3)
Ignorecase:- Property is used to specify
case sensitiveness
. Syntax:-
set r=new regexp
r.pattern=”[A-Z][a-z]+”
r.global=true
r.ignorecase=true
Methods Examples:
1) Execute( ):- Method is used to apply given pattern on given text and
find matched values in text.
Ex:-
set rg=new regexp
rg.pattern=”[0-5]+”
rg.global=true
st=”mno123def 34 zsdfg45gh”
set x=rg.execute(st)
for each y in x
print(y.value)
next
2)
Test:-Method is used to apply given
pattern on given text for matching. If pattern was matched with
text, then this method returns true.
Ex:-
set r=new regexp
r.pattern=”[0-6]+”
r.global=true
x=”633544”
if r.test(x) then
print(“Data is matched”)
else
print(“Mismatch in data”)
end if
3)
Replace( ):- Method is used to
replace matched text with other text.
Ex:-
set r=new regexp
r.pattern=”[0-5]+”
r.global=true
x=”This is QTP machine 5”
r.replace(x,”I”)
print(x)