Tuesday, 6 October 2015

Regular Expressions In QTP

Regular expressions are used to identify objects and text strings which have varying values. Regular Expression are strings that defines phrase like based on special character provided in the expression. This is useful when expected value of any object property is regularly changing but in a fix pattern. That means the object is dynamic but it changes it’s values according to the point. This is also called as pattern matching.

Some useful points to remember about Regular Expressions are:

  • Regular Expression is useful for following scenarios in QTP:
  • Defining the property values of an object in dialog boxes or in programmatic descriptions
  • Defining expected values for checkpoints
  • Defining pop-up window conditions in a recovery scenario.
  • Parameterize an object property or a check point.


We can create regular expression for strings only.
  • Period (.), hyphen (-), asterisk (*), caret (^), brackets ([ ]),parentheses (()), dollar sign ($), vertical line (|), plus sign (+), question mark (?), and backslash (\) are special characters used to create regular expression.
  • When one of above mentioned special characters is preceded by a backslash (\), QTP treats it as a literal character.
  • By default, the value of all Property objects added to a Properties collection are treated as regular expressions.


Below are the various regular expressions used in QTP:


Matching Any Single Character (.) :

 Example: abc. Will match abc followed by any character.

Matching Any Single Character in a List ( [xy] )

 Example [ab] will match either a or b

Matching Any Single Character Not in a List ( [^xy] ) 

 Example 1[^23] will match all values between 11 to 19 except 12 and 13.

Matching Any Single Character within a Range ( [x-y] )

 Example : 1[1-3] will match 11,12, and 13.

Matching Any Non-AlphaNumeric Character (\W)

 It will match any special character other than underscore. Please note case of W in this case.

Matching Zero or More Specific Characters ( * ) 

This matches zero or more occurrences of the preceding character. Example ca* will match caa,caaaa,c and so on. Similarly c.* will match c, cs,caaa, and so on, since preceding character here is “.”.

Matching One or More Specific Characters ( + ) 

Only difference from * is it will match for minimum one character. Example ta+r will match taar,tar but not tr as in above case.

Matching Zero or One Specific Character ( ? )

 A question mark (?) instructs QTP to match zero or one occurrences of the preceding character. For example: te?r matches ter and tr, but nothing else

Matching One of Several Regular Expressions ( | )  

Example new|day will match either of new or day. If we write ne(w|d)ay, it will match neway or neday.

Matching the Beginning of a Line ( ^ ) 

This will match only if match is found at beginning of line.

Matching the End of a Line ( $ )  

This will match only if match is found at end of line.

Matching a word at boundary(\b)

 Example new\b will match testnew but not in knewit.

Matches a digit character(\d)  

 Matches a digit value.

Matching a non-digit character(\D)


 Matches a non digit value

No comments:

Post a Comment