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, such as "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, such as "a", "b", "1", "2", ...

The table below provides an overview of the most commonly used special symbols.

Special symbol

Description

.

Any single character, such as "a", "1", "/", "?", ".", etc.

\d

Any decimal digit, such as "0", "1", ..., "9".

\D

Any non-digit, that is the same as ".", but excluding "0", "1", ..., "9".

\s

Any white space character, such as " " 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, such as "a", ..., "z", "A", ..., "Z", "0", ..., "9".

\W

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

Examples

  • The pattern ".an" matches all text lengths of three ending with "an", such as "can" and "man" but not "mcan".

  • The pattern "\d\d\s\d\d" matches all text lengths five starting with two digits followed by a white space and ending with two digits, such as "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. If you wish to match exactly the "." character, instead of any single character, you should write "\.".

    For 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 ")".

    For example, the pattern "abc" can be organized as "(a)(bc)".

  • All single characters are considered subpatterns.

    For example, in the pattern "abc", each single character "a", "b", and "c" is considered a subpattern.

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

Operator

Description

?

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.

Examples

  • ".*" matches any text, such as "Design Studio", "1213" and "" (the empty text)

  • "(abc)*" matches any number of repetitions of the text "abc", such as "", "abc", "abcabc", and "abcabcabc", but not "abca"

  • "(\d\d){1,2}" matches either two or four digits, such as "12" and "6789", but not "123"

  • "(good)?bye" matches "goodbye" and "bye"

  • "(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, which is described in Experiment with Expressions.

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"), as 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"), as the *?-operator will match as few repetitions as possible.

We recommend 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.

In the Pattern Editor 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 usually sets the test input text to the text that the pattern is 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 appears in the Output panel.

The Symbol button in the Patter Editor is very useful when you want to enter a special symbol in the pattern. When you click it, a menu is shown, from which you can select the symbol to insert in the pattern. This way, you do not have to memorize all the special symbols and their meanings.

For more on the available special symbols and patterns, see Patterns.