RSS Feed

All About Regular Expressions in QTP

Posted on

What is a Regular Expression?
A regular expression (also known as regexp) is a text that consists of patterns of characters used to identify strings (the string should contain the pattern(s) defined in the regular expression as a sub-string).

Example: A regular expression that represents all the words starting with ‘exp’ can be written as –
exp.* representing words like explain, explore, experience etc.
Regular Expressions and QTP
Regular Expressions can be used in QTP in cases where the user needs to identify objects where part of the object property remains constant & the remaining part changes frequently. In such cases, regular expressions can be used which take into account the property changes in the objects and identify the objects based on the constant (unchanged) part of the property.

Example: In the below image, the number of unread mails (3 in this case) will change constantly. But the text ‘Inbox’ will remain same.


These type of objects are ideal candidates where regular expressions need to be used for object identification.
Using Regular Expressions in QTP
In QTP, regular expressions can be used for object identification both in Object Repository as well as in Descriptive Programming. Using (.*) Regular Expression with Descriptive Programming Approach

Set objDesc = Description.Create
objDesc(“micclass”).Value = “Link”
objDesc(“innertext”).Value = “Inbox (.*)”
‘Click on the Inbox Link
Regular Expression Characters and Usage
Using Backslash Character (): A backslash can be used for two purposes. Firstly, when used with a special character, the special character is treated as a literal character. For example, . is treated as . (period). If backslash is used with characters n, t, w or d, the combination becomes a special character. For example, n is treated as newline character. Other than the above 2 cases, if backslash is used anywhere, then its ignored.
Matching Single Character (.): A dot is used to match a single character other than newline character. The number of dots represent the number of characters that need to be matched.
Example: ca. matches cat, cab, cam, can etc.
s.. matches sit, sat, sip etc.
Matching a Single Character from a List ( [xyz] ): Square brackets instruct QuickTest to search for any single character within a list of characters.
Example: ca[tnp] matches only cat or can or cap.
Matching a Single Character Not in a List ( [^xyz] ):If caret (^) is specified as the first character in a square bracket, it instructs QTP to consider all the characters other than the ones mentioned in the square brackets.
Example: ca[^rbp] matches all 3 letter words starting with ‘ca’ other than car, cab and cap.
Matching a Single Character within a Range ( [x-z] ): To match a single character within a range, we use square brackets ([ ]) with the hyphen (-) character.
Example: To match any year in the 1990′s, we can use 199[0-9]
Matching Zero or More Specific Characters ( * ):An asterisk (*) instructs QuickTest to match zero or more occurrences of the preceding character.
Example: ca*p matches cap, caap, caaaaaaaaap, cp etc.
Matching One or More Specific Characters ( + ):A Plus Sign (+) instructs QuickTest to match one or more occurrences of the preceding character.
Example: ca+p matches cap, caap, caaaaaaaaap etc. but not cp.

Matching Zero or One Specific Character ( ? ): A Question Mark (?) instructs QuickTest to match zero or one occurrences of the preceding character.

Example: ca?p matches only cp and cap.Matching Fixed Number of Specific Characters ( {n} ): {n} instructs QTP to match ‘n’ occurrences of the character before opening curly bracket.
Example: ca{5}t matches caaaaat only.
c.{5}t matches caaaaat, ckjhdgt, cgjhdet, cuuikdt etc.
Grouping Regular Expressions ( ( ) ): Parentheses (()) instruct QuickTest to treat the contained sequence as a unit, just as in mathematics and programming languages.
Matching One Or More Regular Expressions ( | ): A vertical line (|) instructs QuickTest to match one of a choice of expressions.
Example: cat|mat matches either cat or mat.
cat|mat|rat matches either cat or mat or rat.
ca(t|m|r)at matches catat or camat or carat.
Matching the beginning of the line ( ^ ): A caret (^) instructs QuickTest to match the expression only at the start of a line, or after a newline character.
Example: ^car matches ‘car is red’. But it doesn’t match ‘red car’.
Matching the End of the line ( $ ): A dollar sign ($) tells QTP to match the expression only at the end of the line.
Example: car$ doesn’t match ‘car is red’. But it matches ‘red car’.
Matching any Alpha-Numeric Character including Underscore ( w ): w instructs QTP to match any alpha-numeric character including underscore(_). Alpha-Numeric characters include A-Z, a-z and 0-9.
Example: w matched ab_Cd, abc_d, ABCD_, -abCD etc.Matching any Non Alpha-Numeric Character excluding Underscore ( W ): W matches all the characters other than alpha-numeric characters and underscore.
Example: W matches @#, !@$%, ^&%* etc.Matching at least ‘n’ times ( {n,}  ) times: {n,} instructs QTP to match the preceding at least ‘n’ number of times.
Example: ca{2,}t means that ‘a’ should come 2 or more times in the string. That is, caat, caaaat, caaaaaaat matches but cat doesn’t match.Matching at least ‘n’ times and at most ‘m’ times ( {n,m} ) [where n: {n,m} instructs QTP to match the preceding character at least n times and at the most m times.
Example: ca{2,4}t means that the letter a can occur a minimum of 2 times and a maximum of 4 times. That is, it matched caat, caaat and caaaat but doesnt match cat, caaaaat etc.Matching a Word Boundary ( b ): b instructs QTP to match the text at the end of the word.
Example: (okb) matches book but it doesn’t match booking.

Matching a Nonword Boundary ( B ): B instructs QTP to match text at non-word boundary.
Example: (okB) doesn’t match Book but it matches Booking.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: