Search code examples
regexprocmail

Regular expression for strings not containing brackets


I am trying to edit my .procmailrc file to get rid of junk.

The account is from the CS departments at my school, all the emails within the department have subjects with brackets in them for instance:

[Majors] Some Subject

or:

[Undergrads] Another Subject

I get a LOT of spam not caught by the filters but almost all of them don't include brackets. I want to move these emails into a folder for spam but can't figure out the REGEX. Any regex masters out there that can help?


Solution

  • The simple Procmail recipe for Subject: headers which do not contain an opening square bracket is simply

    :0   # Add a second colon if the spam box is an mbox folder
    * ^Subject:[^[]*$
    spam
    

    This simply checks for an opening square bracket and ignores the possibility that you could receive legitimate mail which only contains a closing square bracket (I think it's spammy by itself anyway).

    ... But for completeness, I'll mention that if you want a closing square bracket in there, that's a special case which requires a particular ordering in the character class (just like a dash needs to go first or last to disambiguate it from a character range). A commonly seen character class of this type is [^][] which is a negated character class containing closing square bracket and an opening square bracket. They need to be precisely in this order for it to work; in any other position, the closing square bracket would close the character class. (You can't backslash-escape it in there either; backslashes in character classes are literal.)