Geeking about

Matching Headers With Regular Expressions Using Afew

This post is part of my notmuch of a journey series.

notmuch is somewhat limited in the choice of headers it processes, despite that being an oft requested feature. However afew parses the entire email itself so it can do what it likes.

The recently added HeaderMatchingFilter will match a regular expression you provide against an arbitrary header you specify. The text matched can be used as the tag. A simple version of the ListMailsFilter could be implemented as:

header = List-Id
pattern = <(?P<list_id>.*)>
tags = +lists +{list_id}

So if the List-Id header field is found, and it starts and ends with <> then the contents will be used as a tag, and in addition the tag list_id will be added to the message.

My initial motivation was that we use email addresses of the form:


for various projects, so I wanted to use the projectname as the tag without having to write a filter for every project. With HeaderMatchingFilter I can do:

header = To
pattern = (?P<team_name>[a-z0-9]+)
tags = +{team_name}

header = Cc
pattern = (?P<team_name>[a-z0-9]+)
tags = +{team_name}

Also, we use redmine heavily. It adds a header saying what project the email is associated with. I can use that by doing:

header = X-Redmine-Project
pattern = (?P<project>.*)
tags = +{project}

On a related note, I have now written more documentation for afew, so hopefully the path after me will be smoother.