PCRE_TABLE

NAME

SYNOPSIS

DESCRIPTION

The Postfix mail system uses optional tables for address rewriting or mail routing. These tables are usually in dbm or db format.

Alternatively, lookup tables can be specified in Perl Compatible Regular Expression form. In this case, each input is compared against a list of patterns, and when a match is found the corresponding result is returned.

To find out what types of lookup tables your Postfix system supports use the postconf -m command.

To test lookup tables, use the postmap -fq command as described in the SYNOPSIS above.

TABLE FORMAT

The general form of a PCRE table is:
/pattern/flags result

When pattern matches the input string, use the corresponding result value.

!/pattern/flags result

When pattern does not match the input string, use the corresponding result value.

if /pattern/flags

endif

Match the input string against the patterns between if and endif, if and only if the input string also matches pattern. The if..endif can nest.

Note: do not prepend whitespace to patterns inside if..endif.

if !/pattern/flags

endif

Match the input string against the patterns between if and endif, if and only if the input string does not match pattern. The if..endif can nest.

blank lines and comments

Empty lines and whitespace-only lines are ignored, as are lines whose first non-whitespace character is a ‘#’.

multi-line text

A logical line starts with non-whitespace text. A line that starts with whitespace continues a logical line.

Each pattern is a perl-like regular expression. The expression delimiter can be any character, except whitespace or characters that have special meaning (traditionally the forward slash is used). The regular expression can contain whitespace.

By default, matching is case-insensitive, and newlines are not treated as special characters. The behavior is controlled by flags, which are toggled by appending one or more of the following characters after the pattern:
i (default: on)

Toggles the PCRE_MULTILINE flag. When this flag is on, the ^ and $ metacharacters match immediately after and immediately before a newline character, respectively, in addition to matching at the start and end of the subject string.

s (default: on)

Toggles the PCRE_DOTALL flag. When this flag is on, the . metacharacter matches the newline character. With Postfix versions prior to 20020528, The flag is off by default, which is inconvenient for multi-line message header matching.

x (default: off)

Toggles the pcre extended flag. When this flag is on, whitespace in the pattern (other than in a character class) and characters between a # outside a character class and the next newline character are ignored. An escaping backslash can be used to include a whitespace or # character as part of the pattern.

A (default: off)

Toggles the PCRE_ANCHORED flag. When this flag is on, the pattern is forced to be "anchored", that is, it is constrained to match only at the start of the string which is being searched (the "subject string"). This effect can also be achieved by appropriate constructs in the pattern itself.

E (default: off)

Toggles the PCRE_DOLLAR_ENDONLY flag. When this flag is on, a $ metacharacter in the pattern matches only at the end of the subject string. Without this flag, a dollar also matches immediately before the final character if it is a newline character (but not before any other newline characters). This flag is ignored if PCRE_MULTILINE flag is set.

U (default: off)

Toggles the ungreedy matching flag. When this flag is on, the pattern matching engine inverts the "greediness" of the quantifiers so that they are not greedy by default, but become greedy if followed by "?". This flag can also set by a (?U) modifier within the pattern.

X (default: off)

Toggles the PCRE_EXTRA flag. When this flag is on, any backslash in a pattern that is followed by a letter that has no special meaning causes an error, thus reserving these combinations for future expansion.

SEARCH ORDER

Patterns are applied in the order as specified in the table, until a pattern is found that matches the input string.

Each pattern is applied to the entire input string. Depending on the application, that string is an entire client hostname, an entire client IP address, or an entire mail address. Thus, no parent domain or parent network search is done, and user@domain mail addresses are not broken up into their user and domain constituent parts, nor is user+foo broken up into user and foo.

TEXT SUBSTITUTION

Substitution of substrings from the matched expression into the result string is possible using the conventional perl syntax ($1, $2, etc.). The macros in the result string may need to be written as ${n} or $(n) if they aren’t followed by whitespace.

Note: since negated patterns (those preceded by !) return a result when the expression does not match, substitutions are not available for negated patterns.

EXAMPLE SMTPD ACCESS MAP

# Bounce friend@whatever, except when whatever is our domain (you would
# be better just bouncing all friend@ mail - this is just an example).
/^(friend@(?!my\.domain$).*)$/ 550 Stick this in your pipe $1

# A multi-line entry. The text is sent as one line.
#
/^noddy@my\.domain$/
550 This user is a funny one. You really don’t want to send mail to
them as it only makes their head spin.