Rules using regular expressions

Normally, rules for processing email look for text that "contains," "starts with," or is "equal to" a certain value. We also support more complicated matching using globs or regular expressions. These can be good options for more technical users looking to create complex rules.

You can create rules using regular expressions by going to Settings → Mail rules, clicking the Create rule button, and clicking the Switch to no-preview rules (regular expressions supported) link. Rules can't be previewed when using this option, and this option is recommended for more advanced users. For more information on creating rules, please see our Mail rules help page.

Glob patterns

In a glob pattern, you can use * to match any number of characters, and ? to match a single character. The glob match is "anchored" at the beginning and end of the searched text, so *word* is equivalent to "contains word," while word is equivalent to "is word."

Regular expressions

A regular expression is a powerful system for testing a value for a match, going well beyond what a glob match supports.

This is a complex system, and the scope of regular expressions is too broad to be covered here. If you'd like to learn about regular expressions, there are many good resources available online.

Note that POSIX EREs are supported, but not PCREs.

Negation

Be careful using does not match glob and does not match regular expression conditions. It's easy to accidentally filter all your mail away if the conditions have not been fully considered.

If you attempt to build a whitelist with multiple discard rules, it's particular easy to end up discarding all mail. As an example, suppose you had the following two discard rules:

  • "The sender's email does not match glob *myfriend*."
  • "The sender's email does not match glob *myotherfriend*."

All mail would be rejected. The first rule will discard mail from "myotherfriend@example.com," and the second rule will discard mail from "myfriend@example.com."

Note that the * at either end are important if you just want to test that some text doesn't occur anywhere in the specified headers.

Was this article helpful?
33 out of 109 found this helpful