Regular Expressions:


A Regular Expression is a string that describes or matches a set of strings, according to certain syntax rules. A Regular Expression often called a pattern is an expression that describes a set of strings.

Regular Expressions enable QuickTest to identify objects and text strings with varying values.

We can use Regular Expressions in the below situations,
1.    Property Value for a particular object is varying frequently.
2.    Parameterizing a step.
3.    Creating checkpoints with varying values.

Using the Backslash Character,

A backslash (\) instructs QuickTest to treat the next character as a literal character, if it is otherwise a special character.

The backslash (\) can also instruct QuickTest to recognize certain ordinary characters as special characters. Example: QuickTest recognizes \n as the special newline character.

Example:

\\ matches the literal character \
\ (matches the literal character (

Matching Any Single Character:

A period (.) instructs QuickTest to search for any single character (except for \n).

          For example:        

          Welcome. matches Welcomes, Welcomed, or Welcome followed by a space or any other single character.

A series of periods indicates the same number of unspecified characters.

To match any single character including \n, enter:  (.|\n)

Matching Any Single Character in a List:   

Square brackets instruct QuickTest to search for any single character within a list of characters.

Example, to search for the date 2000, 2001, or 2002, enter:  200[012]

Matching Any Single Character Not in a List Iterative constructs:

When a caret (^) is the first character inside square brackets, it instructs QuickTest to match any character in the list except for the ones specified in the string.

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

          Note: The caret has this special meaning only when it is displayed first within the brackets.

Matching Any Single Character within a Range   

In order to match a single character within a range, you can use square brackets ([ ]) with the hyphen (-) character.

Example: To match any year in the 2000s, enter: 200[0-9]

Matching Zero or More Specific Characters Functions and subroutines

An asterisk (*) instructs QuickTest to match zero or more occurrences of the preceding character.

For example:  ca*r          matches       car, caaaaaar, and cr.

Matching One or More Specific Characters

A plus sign (+) instructs QuickTest to match one or more occurrences of the preceding character.

Example: ca+r     matches car and caaaaaar, but not cr.

Matching Zero or One Specific Character  

A question mark (?) instructs QuickTest to match zero or one occurrences of the preceding character.

Example: ca?r     matches car and cr, but nothing else.

Grouping Regular Expressions

                   Parentheses (()) instruct QuickTest 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 ( * , + , ? , { } ).

Matching One of Several Regular Expressions

                   A vertical line (|) instructs QuickTest to match one of a choice of expressions.

 For example:

      Too|bar     causes QuickTest to match either Too or bar.
      To(o|b)ar   causes QuickTest to match either Tooar or Tobar.

Matching the Beginning of a Line:    

A caret (^) instructs QuickTest to match the expression only at the start of a line, or after a newline character.

Example:

      Phone    matches Phone within the lines—Phone, my Phone, and Phone list, while
    ^ Phone  matches Phone only in the lines—Phone and Phone Numbers.

Matching the End of a Line

          A dollar sign ($) instructs QuickTest to match the expression only at the end of a line, or before a newline character.
Example:  Phone$  matches Phone only in the line—my Phone.

Matching Any Alphanumeric Character Including the Underscore:  

\w instructs QuickTest to match any alphanumeric character and the underscore (A-Z, a-z, 0-9, _).

Example:

     \w* causes QuickTest to match zero or more occurrences of the alphanumeric characters—A-Z, a-z, 0-9, and the underscore (_). It matches xyz, p8aj, or 1_uLeu_4.
     \w{3} causes QuickTest to match 3 occurrences of the alphanumeric characters A-Z, a-z, 0-9, and the underscore (_). It matches xy4, c7_, or p_n.

Matching Any Non-AlphaNumeric Character

\W instructs QuickTest to match any character other than alphanumeric characters and underscores.

Example:     \W matches &, *, ^, %, $, and #.

Combining Regular Expression Operators:

You can combine regular expression operators in a single expression to achieve the exact search criteria you need.

Example: start.*     matches start, started, starting, starter, etc.

No comments:

Post a Comment