Regular Expressions in Mailman

Last updated: November 21, 2024

Regular expressions explained

Regular expressions are used within the Python programming language (which is what Mailman is written in) and other programming languages to specify rules for matching strings of text. In other words, you can use regular expressions to match text in incoming email messages. The settings in which you use the regular expressions will tell Mailman what to do with messages that have matching text.

The settings in Mailman that will accept regular expressions will also accept non-regular expression text so there needs to be a way for Mailman to know whether the text that is entered is a regular expression or not. To specify that the text is a regular expression, begin the line with ^.

Common uses of regular expressions in Mailman

Specify domains that are allowed to post

The most common use of regular expressions in Mailman is to specify entire domains that can and cannot post messages to your list. For example, to allow all addresses in the washington.edu domain, even those addresses that are not subscribed, to post messages without moderation to your list, go to Privacy Options and then the Sender filters sub-category and enter the following regular expression in the accept_these_nonmembers setting:

^.*(washington|uw)\.edu$

The ^ specifies that the string is a regular expression. The .* specifies that any text that comes before the “washington.edu” part of the email address of the sender is acceptable. For example, bob@u.washington.edu, joe@cac.washington.edu, and sally@ocean.washington.edu will all match the regular expression. The \ specifies that the subsequent period is part of the text of the string you want to match and not a special character that can be used in regular expressions. The $ specifies that it is the end of the string.

Please be aware that spammers often forge the headers of spam email. If the forged From header is set to an email address ending in washington.edu, that message will be able to get through to your list. If you are concerned about spam on your list, see the How can I avoid spam on my Mailman list? FAQ.

Specify domains that are not allowed to post

To ban an entire domain from being able to post to your list and have Mailman automatically discard all messages from that domain, enter the following regular expression in the discard_these_nonmembers setting on the Privacy Options category → Sender filters subsection of the admin pages:

^.*domain$

where “domain” is the domain you want to block.

Ban non-UW email addresses from subscribing

If you want to ban all non-UW email addresses from subscribing to your list, enter the following regular expression in the ban_list setting on the Privacy Options category → Subscription Rules subsection of the admin pages:

^(?!.*(washington|uw)\.edu$)

As you can see, this regular expression is a little more complex than the preceding ones. This regular expression is known as a negative lookahead and is telling Mailman to match subscribing email addresses that don’t end in “washington.edu” and to ban those matching email addresses.

Other uses

There are many more uses of regular expressions. This page was not designed to go into all of the uses; it is only meant to give you a simple overview of how regular expressions are used in Mailman. For more information on specific settings that may use regular expressions, look through the other Mailman help pages on this website, especially the Frequently Asked Questions. While not necessarily mentioning the term “regular expression,” the other pages still contain information on how to accomplish specific tasks by using regular expressions.

For more general information on regular expressions within Python, see Python’s Regular Expression Operations and the Regular Expression HOWTO.

Settings that use regular expressions

The following settings in Mailman will accept regular expressions: