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

You can use the following special symbols within a pattern.

Special Symbol Description

.

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

\d

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

\D

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

\s

Any whitespace character, e.g. " ", tab, and return

\S

Any non-whitespace character, e.g. same as "." excluding " ", tab, and return

\w

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

\W

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

\n

A line break character.

\r

A carriage return character.

\t

A 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|b

Matches 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.

Repeating Operators

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

Special Symbol Description

{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.

These operators repeat the previous character, symbol, or subpattern.

More About 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.