Chapter 8 - Regular expressions
Exim supports the use of regular expressions in many of its options. It uses the PCRE2 regular expression library; this provides regular expression matching that is compatible with Perl 5. The syntax and semantics of regular expressions is discussed in online Perl manpages, in many Perl reference books, and also in Jeffrey Friedl’s Mastering Regular Expressions, which is published by O’Reilly (see http://www.oreilly.com/catalog/regex2/).
The documentation for the syntax and semantics of the regular expressions that are supported by PCRE2 is included in the PCRE2 distribution, and no further description is included here. The PCRE2 functions are called from Exim using the default option settings (that is, with no PCRE2 options set), except that the PCRE2_CASELESS option is set when the matching is required to be case-insensitive.
In most cases, when a regular expression is required in an Exim configuration, it has to start with a circumflex, in order to distinguish it from plain text or an “ends with” wildcard. In this example of a configuration setting, the second item in the colon-separated list is a regular expression.
domains = a.b.c : ^\\d{3} : *.y.z : ...
The doubling of the backslash is required because of string expansion that precedes interpretation – see section 11.1 for more discussion of this issue, and a way of avoiding the need for doubling backslashes. The regular expression that is eventually used in this example contains just one backslash. The circumflex is included in the regular expression, and has the normal effect of “anchoring” it to the start of the string that is being matched.
There are, however, two cases where a circumflex is not required for the recognition of a regular expression: these are the match condition in a string expansion, and the matches condition in an Exim filter file. In these cases, the relevant string is always treated as a regular expression; if it does not start with a circumflex, the expression is not anchored, and can match anywhere in the subject string.
In all cases, if you want a regular expression to match at the end of a string, you must code the $ metacharacter to indicate this. For example:
domains = ^\\d{3}\\.example
matches the domain 123.example, but it also matches 123.example.com. You need to use:
domains = ^\\d{3}\\.example\$
if you want example to be the top-level domain. The backslash before the $ is needed because string expansion also interprets dollar characters.