Regular Expressions in QTP ? Types of Regular Expressions?

It is a Way of Representing data using symbols. they are often used within matching, searching or replacing algorithams.

Regular Expressions enable Quick Test to identify objects and text strings with varying values.Regular expressions are used when the objects value is dynamically changed.

A regular expression is a string that specifies a complex search phrase. By using special characters such as a period (.), asterisk (*), caret (^), and brackets ([ ]), etc. you define the conditions of the search.

Where we can use regular expressions in QTP

  • Defining Properties of an Object in Descriptive Programming /Object Repository for handling dynamic objects.
  • Verifying data using check points
  • Parameterize an object property or check point
Types of Regular Expressions:

1. Using the Backslash Character: A backslash (\) can serve two purposes. It can be used in conjunction with a special character to indicate that the next character be treated as a literal character. For example, \. would be treated as period (.) instead of a wildcard. Alternatively, if the backslash (\) is used in conjunction with some characters that would otherwise be treated as literal characters, such as the letters n, t, w, or d, the combination indicates a special character. For example, \n stands for the newline character.

2. Matching Any Single Character: A period (.) instructs QTP to search for any single character (except for \n).

Ex: welcome. 
Matches welcomes, welcomed, or welcome followed by a space or any other single character

3. Matching Any Single Character in a List: Square brackets instruct QTP to search for any single character within a list of characters.
Ex: To search for the date 1867, 1868, or 1869
Syntax: 186[789]

4. Matching Any Single Character Not in a List: When a caret (^) is the first character inside square brackets, it instructs QTP to match any character in the list except for the ones specified in the string.

Example:  [^ab] 
Matches any character except a or b.

5. Matching Any Single Character within a Range: To match a single character within a range, you can use square brackets ([ ]) with the hyphen (-) character.

Example: For matching any year in the 2010s, 
Syntax: 201[0-9]

6. Matching Zero or More Specific Characters: An asterisk (*) instructs QTP to match zero or more occurrences of the preceding character.

For example: ca*r
Matches car, caaaaaar, and cr

7. Matching One or More Specific Characters: A plus sign (+) instructs QTP to match one or more occurrences of the preceding character.

For example: ca+r 
Matches car and caaaaaar, but not cr.

8. Matching Zero or One Specific Character: A question mark (?) instructs QTP to match zero or one occurrences of the preceding character.

For example: ca?r 
Matches car and cr, but nothing else.

9. Grouping Regular Expressions: Parentheses (()) instruct QTP to treat the contained sequence as a unit, just as in mathematics and programming languages. Using groups is especially useful for delimiting the argument(s) to an alternation operator ( | ) or a repetition operator ( * , + , ? , { } ).

10. Matching One of Several Regular Expressions: A vertical line (|) instructs QTP to match one of a choice of expressions.

11. Matching the Beginning of a Line: A caret (^) instructs QTP to match the expression only at the start of a line, or after a newline character.

12. Matching the End of a Line: A dollar sign ($) instructs QTP to match the expression only at the end of a line, or before a newline character.

13. Matching Any AlphaNumeric Character Including the Underscore: \w instructs QTP to match any alphanumeric character and the underscore (A-Z, a-z, 0-9, _).

14. Matching Any Non-AlphaNumeric Character: \W instructs QTP to match any character other than alphanumeric characters and underscores.

15. Combining Regular Expression Operators: You can combine regular expression operators in a single expression to achieve the exact search criteria you need.

For example: start.* 
Matches start, started, starting, starter, and so forth. 
we can use a combination of brackets and an asterisk to limit the search to a combination of non-numeric characters. 

For example: [a-zA-Z]* 
To match any number between 0 and 1200, we need to match numbers with 1 digit, 2 digits, 3 digits, or 4 digits between 1000-1200. 
The regular expression below matches any number between 0 and 1200. 

To receive daily posted JOBS & Interview Questions
Just enter your email address below and click 'Submit'
Enter your email address:

Make sure to activate your subscription by clicking on the activation link sent to your email