Patterns

A pattern is a 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". We say that these texts match the 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", ...

Special Symbols

Within a pattern, the following special symbols can be used.

Special SymbolMeaning
.any single character, e.g. "a", "1", "/", "?", "." etc.
\dAny decimal digit, e.g. "0", "1", ..., "9".
\DAny non-digit, e.g. same as "." excluding "0", "1", ..., "9".
\sAny whitespace character, e.g. " ", tab, and return
\SAny non-whitespace character, e.g. same as "." excluding " ", tab, and return
\wAny word (alphanumeric) character, e.g. "a", ..., "z", "A", ..., "Z", "0", ..., "9".
\WAny non-word (alphanumeric) character, e.g. same as "." excluding "a", ..., "z", "A", ..., "Z", "0", ..., "9".
\nA line break character.
\rA carriage return character.
\tA tab character.
[abc]Any character in the set a, b or c.
[^abc]Any character not in the set a, b or c.
[a-z]Any character in the range a to z, inclusive.
a|bMatches whatever the subpattern a would match, or whatever the subpattern b would match.

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 "\.".

You can organize a pattern into subpatterns by the use of parentheses: "(" and ")". The pattern "abc" can be organized as "(a)(bc)". Subpatterns are useful when applying pattern operators.

Simple Example Patterns

Here are some examples of patterns and what they match:

PatternMatches
.an All texts of length three ending with "an", e.g. "can" and "man" but not "mcan".
\d\d\s\d\d All texts of length five starting with two digits followed by a whitespace and ending with two digits, e.g. "01 23" and "72 13" but not "01 2s"
m\.n\\o The text "m.n\o"
(good)|(bye) "good" and "bye" but not "goodbye"

Repeating Operators

These operator symbols will repeat the previous character, symbol, or subpattern.

Special SymbolMeaning
{m} Matches exactly m repetitions of the preceding subpattern.
{m,n} Matches between m and n repetitions (inclusive) of the preceding subpattern. It will match as many subpatterns as possible.
{m,n}? Matches between m and n repetitions (inclusive) of the preceding subpattern. It will match as few subpatterns as possible
{m,} Matches m or more repetitions of the preceding subpattern. It will match as many subpatterns as possible.
{m,}? Matches m or more repetitions of the preceding subpattern. It will match as few subpatterns as possible.
? The preceding subpattern, or the empty text. Shorthand for {0,1}
* Matches any number of repetitions of the preceding subpattern, or the empty text. Shorthand for {0,}. It will match as many subpatterns as possible.
*? Matches any number of repetitions of the preceding subpattern, or the empty text. Shorthand for {0,}?. It will match as few subpatterns as possible.
+ Matches one or more repetitions of the preceding subpattern. Shorthand for {1,}. It will match as many subpatterns as possible.
+? Matches one or more repetitions of the preceding subpattern. Shorthand for {1,}?. It will match as few subpatterns as possible.

Examples Using Repeating Operators

Here are some examples of patterns that use repeating operators, and what they match:

PatternMatches
.* Any text, e.g. "hello", "1213" and "" (the empty text)
(abc)* Matches any number of repetitions of the text "abc", e.g. "", "abc", "abcabc", and "abcabcabc", but not "abca"
(.*)(.*) Will match "abc" - the first subpattern will match "abc" and the second subpattern will match "" (the empty text)
(.*?)(.*) Will match "abc" - the first subpattern will match "" (the empty text) and the second subpattern will match "abc"
(.+?)(.*) Will match "abc" - the first subpattern will match "a" and the second subpattern will match "bc"
\w*\d Will match "abc1abc1" - \w* matches "abc1abc" and \d matches "1"
\w*?\d Will match "abc1" but not "abc1abc1" - because the "\w*?" will only match "abc" and the rest cannot be matched by \d
(\d\d){1,2} Matches either two or four digits, e.g. "12" and "67", but not "123".
(good)?bye "goodbye" and "bye".

More on Grouping

We have seen that "(" and ")" can be used for grouping subpatterns into a new subpattern. But this actually serves another purpose: you can use these groups in expressions. To make a grouping that cannot be used in expressions you can use "(?:".

You can refer to other groups in your pattern using \n, where n is the group number.

Grouping Examples

Here are some examples of patterns that use grouping, and what they match:

PatternMatches
a(bc) Matches "abc", and you can refer to "bc" as $1 in your expressions.
a(?:bc) Matches "abc", but you can not refer to "bc" in your expressions.
(.*)=\1 Matches "abc=abc", but does not match "abc=ab"

Videos

Following is a video giving an introduction to patterns.