Professional Documents
Culture Documents
Regular Expressions
Regular Expressions
• Case Study
• Session Wrap-Up
• A regular expression is a string that describes or matches a set of strings, according to certain syntax
rules.
or
A regular expression, often called a pattern, is an expression that describes a set of strings. They are
usually used to give a concise description of a set, without having to list all elements.
• Regular expressions are used by many text editors and utilities to search and manipulate bodies of
text based on certain patterns.
• The set of utilities (including the editor sed and the filter grep) provided by Unix distributions were the
first to popularize the concept of regular expressions.
• The origin of regular expressions lies in Automata Theory and formal language theory (both part of
Theoretical Computer Science.
Common Scenarios
1. 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 (
• To indicate that the period is not part of a regular expression, you would enter it as
follows:
mercurytours\.mercuryinteractive\.com
• A period (.) instructs QuickTest to search for any single character (except for \n).
For example:
Welcome.
• Square brackets instruct QuickTest to search for any single character within a list of characters.
200[012]
• 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]
Note: The caret has this special meaning only when it is displayed first within the brackets.
• In order to match a single character within a range, you can use square brackets ([ ])
with the hyphen (-) character.
200[0-9]
• 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.
• 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.
• Parentheses (()) instruct QuickTest to treat the contained sequence as a unit, just as
in mathematics and programming languages.
• For example:
Too|bar causes QuickTest to match either Too or bar.
To(o|b)ar causes QuickTest to match either Tooar or Tobar.
• 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.
• 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.
• \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.
• Example:
\W matches &, *, ^, %, $, and #.
• 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.
• Strings following the specified pattern will get stored in submatches collection when
regular expression is executed.
Problem: Fetch submatch email-ids strings which follows the following the regular
expression pattern "\w{6}.\w{6}@\w{9}.\w{3}“.
• Used to change value of a property in a predictable way during each run session.
• By default, the value of all Property objects added to a Properties collection are treated as regular
expressions.
• Use the RegularExpression property to change this setting for a specific Property object in the
collection.
• Syntax
PropertiesColl(Property).RegularExpression = BooleanSetting
Property Variant The property for which you want to specify the regular
expression value. Specify the name of the property or its
position in the collection.
• Example
The following example uses the RegularExpression property to set the value of the PropName Property
object as a literal value.
set MyDesc = Description.Create()
MyDesc("PropName").Value = PropValue
MyDesc("PropName").RegularExpression = FALSE
• For Example : If we want to change the name of userName WebEdit control to LoginName then following code should
be added.
• Problem: Consider a site includes a form in which the user inputs data and
clicks the Send button to submit the form.
• When a required field is not completed, the form is displayed again for the
user to complete.
• When resubmitting the form, the user clicks the Resend button.
• When creating a text checkpoint to check that a varying text string is displayed
on your Web site or application, you can define the text string as a regular
expression.
• Case Study
Problem : Check that every window and dialog box in the application contains
the name of the application followed by a hyphen (-) and a descriptive title.
Solution : Add a checkpoint to each dialog box object in the test to check that
the first part of the title contains the name of the application followed by a
hyphen. .
Case Study : 1
• Problem:
• Create a text checkpoint on a date text string that changes according to the selected flight
date.
• Solution:
Define the date as a regular expression so that the checkpoint checks that the captured
text string matches the expected format, rather than checking the exact text.
In the Active Screen, scroll up and highlight the date. Right-click the highlighted string and
select Insert Text Checkpoint. The Text Checkpoint Properties dialog box opens.
This instructs QuickTest to check that each character in the selected text matches the
number-range format defined by the regular expression.
• The checkpoint passed because the text was displayed in the format
specified by the regular expression
• Problem: Consider a site includes a form in which the user inputs data and
clicks the Send button to submit the form.
• When a required field is not completed, the form is displayed again for the user
to complete.
• When resubmitting the form, the user clicks the Resend button.
Questions?
Q & A….
Session Wrap-up