Thursday, November 06, 2014

Using Regular Expressions in Exchange Transport rules

I was recently tasked with creating a Transport Rule that fired if an email's subject line "started with" a string.   Unfortunately, this is not an option in the GUI or powershell with Exchange 2013 or Exchange Online, but you can use regular expressions.

First, here's the link of the different Transport Rule Conditions:
http://technet.microsoft.com/en-us/library/jj919235%28v=exchg.150%29.aspx

If you scan the Description column for "that match the specified regular expression" you will see that many different Transport conditions can use these.

I did some searching, and found this older Exchange 2010 document that details the RegEx support here:
http://msdn.microsoft.com/en-us/office/aa997187%28v=exchg.149%29

(And just in case, I will copy paste it at the bottom of this post as well)

So my customer's rule was very simple:
Choosing a subject matching ^## would select any subject that begun with ##




If you are interested in configuring Message Encryption, it requires Azure RMS licensing, and then following these two articles:



Another really good example of a usage for regex is to match on something like a SS# pattern and block transmitting external to the organization using something like:
If subject or body matches \d\d\d-\d\d-\d\d\d\d


Pattern matching in Exchange Transport Rules

Pattern string Description
\S The \S pattern string matches any single character that's 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.
\W The \W pattern string matches any single Unicode character not categorized as a letter or a decimal digit.
| The pipe ( | ) character performs an OR function.
* The asterisk ( * ) 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.
\ A backslash is used as an escaping character before a special character. Special characters are characters used in pattern strings:
  • Backslash ( \ )
  • Pipe ( | )
  • Asterisk ( * )
  • Opening parenthesis ( ( )
  • Closing parenthesis ( ) )
  • Caret ( ^ )
  • Dollar sign ( $ )
For example, if you want to match a string that contains (525), you would type \(525\).
^ The caret ( ^ ) character indicates that the pattern string that follows the caret must exist at the start of the text string being matched.
For example, ^fred@contoso matches [email protected] and [email protected] but not [email protected].
$ The dollar sign ( $ ) character indicates that the preceding pattern string must exist at the end of the text string being matched.
For example, contoso.com$ matches [email protected] and [email protected], but doesn't match [email protected]



No comments: