
Implementing Regular Expressions
In the Exchange Management Shell, you can use regular expressions in any predicate that accepts the Patterns predicate property. In the Exchange Management Console, you can use regular expressions with any condition or exception that contains the words with text patterns. Table 1 lists all the pattern strings that you can use to create a pattern-matching regular expression.
Caution: |
|---|
|
You must carefully test the regular expressions that you construct to make sure that they yield the expected results. An incorrectly configured regular expression could yield unexpected matches and cause unwanted transport rule behavior. Test your regular expressions in a test environment before you implement them in production.
|
Table 1 Pattern strings
|
Pattern string
|
Description
|
| \S | The \S pattern string matches any single character that is not a space. |
| \s | The \s pattern string matches any single white-space character. |
| \D | The \D pattern string matches any non-numeric digit. |
| \d | The \d pattern string matches any single numeric digit. |
| \w | The \w pattern string matches any single Unicode character categorized as a letter or decimal digit. |
| | | The pipe ( | ) character performs an OR function. |
| * | The wildcard ( * ) character matches zero or more instances of the previous character. For example, ab*c matches the following strings: ac, abc, abbbbc. |
| ( ) | Parentheses act as grouping delimiters. For example, a(bc)* matches the following strings: a, abc, abcbc, abcbcbc, and so on. |
| \\ | Two backslashes indicate that the character that follows the backslashes should be escaped. For example, if you want to match a string that contains \d, you would type \\d. |
| ^ | The caret ( ^ ) character indicates that the pattern string that follows the caret must exist at the start of the text string that is being matched. For example, ^fred@contoso matches fred@contoso.com and fred@contoso.co.uk but not alfred@contoso.com. This character can also be used with the dollar ( $ ) character to specify an exact string to match. For example, ^kim@contoso.com$ matches only kim@contoso.com and does not match anything else, such as kim@contoso.com.au. |
| $ | The dollar ( $ ) character indicates that the preceding pattern string must exist at the end of the text string that is being matched. For example, contoso.com$ matches adam@contoso.com and kim@research.contoso.com, but does not match kim@contoso.com.au. This character can also be used with the caret ( ^ ) character to specify an exact string to match. For example, ^kim@contoso.com$ matches only kim@contoso.com and does not match anything else, such as chris@sales.contoso.com. |
By using Table 1, you can construct a regular expression that matches the pattern of the data that you want to match. Working from left to right, examine each character or group of characters in the data that you want to match. Read the description of each pattern string to determine how it is applied to the data that you are matching. Then, determine which pattern string in Table 1 represents that character or group of characters, and add that pattern string to the regular expression. When you are finished, you will have a fully constructed regular expression.
For example, the following regular expression matches North American telephone numbers in the formats 425 555-0100 and 425.555.0100:
425(\s|.)\d\d\d(-|.)\d\d\d\d
You can expand on this example by adding the telephone format (425) 555-0100, which uses parentheses around the area code. The following regular expression matches all three telephone number formats:
(\\()*\d\d\d(\\)|\s|.)\d\d\d(-|.)\d\d\d\d
You can analyze the previous example as follows:
- (\\()* This portion makes the first parentheses optional. Because the closing parenthesis is also a regular expression delimiter, it must be escaped by using two backslashes \\. The surrounding (()) parentheses group the \\( characters together so that the wildcard character * can act upon the \\( characters to make them optional.
- \d\d\d This portion requires that exactly three numeric digits appear next.
- (\\)|\s|.) This portion requires that an opening parenthesis, a space, or a period exist after the three-digit number. Each character-matching string is contained in the grouping delimiters and is separated by the pipe character. This means that only one of the specified characters inside the grouping delimiters can exist in this location in the string that is being matched.
- \d\d\d This portion requires that exactly three numeric digits appear next.
- (-|.) This portion requires that either a hyphen or period exists after the three-digit number. Because the hyphen and period exist in the grouping delimiters, only one of the two characters can exist in this location in the string that is being matched.
- \d\d\d\d This portion requires that exactly four numeric digits appear next.