Patterns

If you are new to patterns we suggest you watch the introduction video on patterns.

A pattern is a formal way of describing a text. For example, the text "32" can be described as a text containing two digits. However, other texts also contain two digits, e.g. "12" and "00" and can therefore also be described as a text containing two digits. We can express this by the pattern \d\d which is a formal way of expressing that a text must contain two and only two digits (\d is the symbol for a digit). We say that these texts match this pattern. Design Studio patterns follow the Perl5 syntax.

A pattern is composed of normal characters and special symbols. Each special symbol carries its own special meaning. For example, the special symbol "." (dot) means any single character and matches all single characters, e.g. "a", "b", "1", "2", ...

The table below provides an overview of the most commonly used special symbols. For a complete overview of all the special symbols available, see the reference documentation on patterns.

Special symbol

Meaning

.

Any single character, e.g. "a", "1", "/", "?", ".", etc.

\d

Any decimal digit, e.g. "0", "1", ..., "9".

\D

Any non-digit, i.e. same as ".", but excluding "0", "1", ..., "9".

\s

Any white space character, e.g. " " and line break.

\S

Any non-white space character, i.e. same as ".", but excluding white space (such as " " and line break).

\w

Any word (alphanumeric) character, e.g. "a", ..., "z", "A", ..., "Z", "0", ..., "9".

\W

Any non-word (alphanumeric) character, i.e. same as ".", but excluding "a", ..., "z", "A", ..., "Z", "0", ..., "9".

The Most Commonly Used Pattern Special Symbols


Example: The pattern ".an" matches all texts of length three ending with "an", e.g. "can" and "man" but not "mcan".

Example: The pattern "\d\d\s\d\d" matches all texts of length five starting with two digits followed by a white space and ending with two digits, e.g. "01 23" and "72 13" but not "01 2s".

If you want a special character, such as "." or "\", to act as a normal character, you can escape it by adding a "\" (backslash) in front of it. So, if you wish to match exactly the "." character, instead of any single character, you should write "\.".

Example: The pattern "m\.n\\o" only matches the text "m.n\o".

You can organize a pattern into subpatterns by the use of parentheses: "(" and ")".

Example: The pattern "abc" can be organized as "(a)(bc)".

All single characters are considered subpatterns.

Example: In the pattern "abc", each single character "a", "b", and "c" is considered a subpattern.

Subpatterns are useful when applying pattern operators. The table below provides an overview of the pattern operators available.

Operator

Meaning

?

Matches the preceding subpattern, or the empty text.

*

Matches any number of repetitions of the preceding subpattern, or the empty text.

+

Matches one or more repetitions of the preceding subpattern.

{m}

Matches exactly m repetitions of the preceding subpattern.

{m,n}

Matches between m and n repetitions (inclusive) of the preceding subpattern.

{m,}

Matches m or more repetitions of the preceding subpattern.

a|b

Matches whatever the expression a would match, or whatever the expression b would match.

The Pattern Operators


Example: ".*" matches any text, e.g. "Design Studio", "1213" and "" (the empty text).

Example: "(abc)*" matches any number of repetitions of the text "abc", e.g. "", "abc", "abcabc", and "abcabcabc", but not "abca".

Example: "(\d\d){1,2}" matches either two or four digits, e.g. "12" and "6789", but not "123".

Example: "(good)?bye" matches "goodbye" and "bye".

Example: "(good)|(bye)" matches "good" and "bye".

As with other special characters, you can escape the special characters that appear in pattern operators by adding a "\" backslash in front of the character.

Subpatterns are useful when you want to extract specific text pieces from a text. When you make a subpattern using parentheses, you can extract the part of the text that is matched by that subpattern. For example, consider the pattern "abc (.*) def (.*) ghi". This pattern has two subpatterns that are made by means of parentheses. If the pattern is matched against the text "abc 123 def 456 ghi", the first of those subpatterns will match the text "123", and the second subpattern will match the text "456". In an expression (see Expressions), you can refer to these subpattern matches by writing "$1" and "$2". For example, the expression "X" + $1 + "Y"+ $2 + "Z" will produce the result "X123Y456Z". This is a very important extraction technique in Design Studio.

By default, the repetition pattern operators (*, +, {...}) will match as many repetitions of the preceding pattern as possible. You can put a "?" after the operator to turn it into an operator that matches as few repetitions as possible. For example, consider the pattern ".*(\d\d\d).*". If the pattern is matched against the text "abc 123 def 456 ghi", the subpattern "(\d\d\d)" will match the second number in the text ("456"), since the first *-operator will match as many repetitions as possible. If you put a "?" after the *-operator, so that the pattern becomes ".*?(\d\d\d).*", the subpattern "(\d\d\d)" will match the first number in the text ("123"), since the *?-operator will match as few repetitions as possible.

It is recommended that you experiment with patterns on your own. The best way to do this is to launch Design Studio and find a place where you can enter a pattern, such as in the Test Tag action. Then, click the "Edit..." button to the right of the pattern field, to open the Pattern Editor Window shown below.

The Pattern Editor Window

In the Pattern Editor Window, you can enter a pattern and test whether it matches the test input text in the "Input" panel. When you open the window, Design Studio will usually have set the test input text to the text that the pattern will be matched against if the given step is executed on the current input robot state. However, you can also edit the test input text yourself, to try the pattern on other inputs. To test the pattern, click the "Test" button. The result of the matching will then be shown in the "Output" panel.

The "Symbol" button is very useful when you want to enter a special symbol in the pattern. When you click it, a pop-up menu will be shown, from which you can choose the symbol to insert in the pattern. This way, you don't have to memorize all the special symbols and their meanings.

For more on patterns, consult the reference documentation on patterns.