Formulating regular expressions

A regular expression is a pattern describing a certain amount of text. Before configuring a routing rule for a route, you should test the regular expressions using any of the testing engines available online to make sure you have constructed it correctly.

The table that follows describes the basic symbols used in regular expressions and their meanings; there are many tutorials on regular expressions available on the web (for example, the Zytrax.com "info" site, which also includes information on using submatches in your expressions).

Note: Regular expressions must follow a strict notation, different from other notation forms you may use. For example, the operating system shell notation for a wildcard (series of 0 or more characters) is the asterisk character: *, The regular expression equivalent for a wildcard is different: it is a combination of a dot followed by an asterisk, as follows: .*
Table 1. Examples and descriptions of symbols commonly used in regular expressions
Example expression Description
[abc] Match any character listed between brackets. In this case, a or b or c.
[^abc] Match any character except those listed between the brackets. In this case, any characters except a, b and c.
X-X

A range is indicated by two characters (letters or digits) separated by a "-" sign and matches any element within the range, including the first character and the last character. One or more ranges can be specified inside the character class. For example:

  • 2-7 matches any digit from 2 through 7.
  • azA-CE-Z matches all lower case letters and the following upper case letters: A, B, C (A-C), plus all letters from E through Z (E-Z); the letter D is excluded from this set.
. Match any character except new-line
\d One digit from zero through nine, inclusive (equivalent to: [0-9])
\D One non-numeric character
\s One whitespace character
\S One non-whitespace character
\w One word character
\W One non-word character
X+ Match the preceding element (X, in this case) one or more times
X* Match the preceding element (X, in this case) zero or more times
X{n} Match the preceding element (X, in this case) n times
X{n,} Match the preceding element (X, in this case) at least n times
X{n,m} Match the preceding element (X, in this case) at least n times, but no more than m times
(abc)?
Note: Matches "abc" or the empty string
Parentheses indicate an optional match
a|b
Note: Matches "a" or "b"
The | indicates choices (alternate matches)