Regular expression examples

You can replace a recognition property with a regular expression or a numeric range to allow for a pattern-based recognition. The pattern allows for more flexibility in the object recognition. You can convert properties to regular expressions and numeric ranges from within the Verification Point Editor or the object map.

For information about using regular expressions, see Replace an Exact-Match Property with a Pattern.

When you convert a value to a regular expression, you can test it while editing it by using the Regular Expression Evaluator.


Examples of regular expression syntax

These examples are typical use cases for regular expressions.

Anchoring

All of the following examples, starting with the General Example, are unanchored. The patterns in the Regular Expression Evaluator are unanchored by default, which means that the pattern can appear anywhere within the string. The string can contain other characters also. Anchoring works the way the Search feature works in most software programs - searching for the text, by itself or nested within other text. If you want to anchor something you use this syntax:

^ string$

The "^" and the "$" anchor the characters in the string. The "^" represents the beginning of the string and the "$" represents the end, when found at the beginning and end, respectively. The "^" character has this special meaning only when it is the first character in a pattern; the "$" has this meaning only when it is the last character in a pattern.

For example, if you want to verify that a property value has a specific string of characters, confirm that you anchor it. Suppose a label in an order form is "Order" if the customer has only one order and is "Orders" if the customer has multiple orders, and you want to confirm that this customer has only one order. On the text property of the label, change the value to a regular expression:

^Order$

In this case, "Order" is the only value that matches. "Orders" does not match.

General Example

Notepad

Suppose you want to determine that an application, such as Notepad, was open. You can use a regular expression on the caption property to verify that the word "Notepad" appears in the banner. This pattern requires the word "Notepad" to appear anywhere within the value. If the text property for the caption is "Report.txt - Notepad", meaning Notepad is open with a file called Report.txt, the pattern matches because it contains the string "Notepad".

The default behavior is to match a value that is contained within a larger string. To match an exclusive set of characters, use the anchoring tags described above.

Case sensitivity

[cC]ustomer

This pattern allows any text that contains the word "customer" with either an uppercase or lowercase "c" will pass. This is important because the comparisons are case-sensitive by default.

In the Regular Expression Evaluator, you can set an option for case sensitivity. The Perform Case Sensitive Match option is the default; matching is case-sensitive and this example shown is useful. To ignore case, clear the check box. You can also set case sensitivity in the interface of the object map or the Verification Point Editor and Comparator. In those tools, when you right-click a regular expression value, you can click Case Sensitive Regular Expression in the shortcut menu.

Any Single Character

payment.method

Use the "." to indicate any single character in the string. In addition to letters and other legal characters, the character can be a space or a numeral. In this case, "payment method" and "payment0method" both match.

Zero or more repeat Characters

fo*bar

Use the "*" character to indicate any number of the previous character, or zero characters. In this example, "fbar" passes; "fobar", "foobar1", and "fooooobar" pass.

One or More Repeat Characters

fo+bar

Use the "+" character to indicate any number of the previous character. The difference between this and the "*" character is that have at least one of the character with the "+". So "fbar", does not match this pattern; but "fobar", "foobar1", and "fooooobar" do match.

Zero or One Repeat Character

fo?bar

This pattern means that either no character, or one of the character before the symbol passes. Only two strings match this syntax: "fbar" and "fobar".

Wildcard

Customer.*Order

Use the "." and "*" characters for a wildcard match. This pattern allows any number of any characters to appear. For example, if an application sometimes inserts a space between the two words in this property, this syntax covers both cases.

Any One Character from a Set

Form[ABC]

This pattern allows the word "Form" followed by any of the characters within the brackets to match. If a field in your application lists the form used, you can use this regular expression to match these strings: "FormA", "FormB", or "FormC".

Any Number of Characters from a Set

Form[ABC]*

This means that the word "Form" followed by any number of one of the characters within the brackets would match this regular expression. If you had a field in your application that would list which form was being used, the examples shown above would still pass: "FormA", "FormB", or "FormC". In addition, multiples of a character would match, such as "FormAA" or "FormCCC", and so would just "Form".

Any One Character(s) Not

Form[^BE]

This syntax allows the word "Form" followed by any character except "B" or "E" to match. For example, "FormA", "FormC", and "FormG" pass, but "FormB" or "FormE" do not pass.

Alphanumeric Set

Form[A-G]

You can specify a range of contiguous letters. In the syntax here, "FormA", "FormB", "FormC", "FormD", "FormE", "FormF", and "FormG" pass, but "FormH" or "FormM" do not pass. The alphanumeric range is case-sensitive. "Forma" does not pass. Use this syntax to match the uppercase and lowercase letters in this range: "Form[a-gA-G]". To match any letter in the standard English alphabet, you use the full ranges for both cases: "Form[a-zA-Z]".

Logical OR

OK|Cancel

You might have a dialog box with three buttons: OK, Cancel, and Help. To verify that the right button was clicked, use a regular expression for the button label property. If you want to confirm that OK or Cancel were clicked, but not Help, you can use this syntax.

Group Expression

My (purple|blue|green) kite

Use a group expression to operate on all elements in a group. For example, to apply an operator to a group or find a specific string before or after each member of the group, you can use a group expression. The parentheses are the grouping operator and you use the "|" to separate the elements. In this example, "My purple kite", "My blue kite", and "My green kite" match the expression. "My red kite" or "My kite" do not match.

Character Classes

You can use a number of character classes in regular expressions.

(single whitespace character, such as space, tab, new line, or carriage return)

Matches letters from languages other than English.

You might want to match an order number. For example:

Order\d

This syntax matches the word "Order" followed by a single digit. The word "Order" alone does not match. These examples match: "Order5", Order3", and "Order0". "Order 3" does not match.

Order\d+

To use more than one digit, use this pattern. The \d+ means one or more digits. For example, "Order77" and "Order235" match this pattern.

Form\S

The word "Form" followed by any character except a space matches. If you need to verify that a form label in your application has a letter indicating the form, this would test for that because if there were no letter after the word "Form", it would not match. For example, "FormB" and "FormX" match. "Form" and "Form " do not match.

The other character classes work in the same manner as these examples.

Special Characters

These examples use certain special characters. These characters are not read as the literal character in the regular expression, but are read as an operator. These include the following characters:

If you use these characters in your regular expression, escape them by preceding the character with a "\". For example, if you need to have parentheses in the text of a label, you might have this text property:

Orders \(shipped\)

In this case, the label "Orders (shipped)" does match. When you convert a value to a regular expression in the Verification Point Editor or the object map, if your original value contains ones of these characters, Functional Tester escapes the characters for you.


Related concepts

Regular Expression Evaluator

Related tasks

Replace an exact-match property with a pattern