Gmail filters are powerful, yet most articles describing how to create powerful filters are not complete. This is my attempt at creating a near-complete guide to Gmail filters. (This is primarily based on my experiments using Gmail filters)
I owe a special thanks to this guy. Much of his experimentation is responsible for this guide.
This article is logically backwards, if you are completely new to Gmail filters or would rather read this article logically I suggest reading from the bottom up.
Examples
This filter captures all email handled by student@college.edu
This filter will additionally capture anything to student.college@gmail.com. This can easily be fixed but is usually sufficient. (to:student OR from:student OR cc:student OR bcc:student) college
This filter captures all email to a particular student group if all of the emails of the group are of the form group-*@college.edu or related-*@college.edu.
* This works because content is split on almost everything, including a -. (to:{group related} OR from:{group related} OR cc:{group related} OR bcc:{group related}) college
Sometimes I get spam from fellow students advertising their group’s event. On occasion they will only use bcc. With this filter it captures those emails that Gmail marks as having undisclosed recipients. -to:{aero asia biz cat com coop edu gov info int jobs mil mobi museum name net org pro tel travel xxx} -list:{aero asia biz cat com coop edu gov info int jobs mil mobi museum name net org pro tel travel xxx}
Occasionally I work on projects with other students. Say that their emails are person1@college.edu and person2@college.edu. And say that I only contact them by addressing both of them in the To: field. I can filter these messages like this: to:person1~person2
I only prefer to use to:, from:, cc:, and bcc: explicitly in order to be sure that nothing will escape my filter
Query Expansion Operators
Symbols in queries will expand.
~#$%^*+;”,<>? act as AND operators
X~Y*Z ≡ X && Y && Z
-=\:’./ will wrap the words on the left and right in quotes
X.Y/Z ≡ “X Y Z” will match X-Y;Z (content is expanded on all symbols except letters, numbers, ampersands, and underscores).
! can be inserted between words to negate them
X!Y ≡ X -Y
The @, ampersand symbol
Is stripped out at the beginning and end of a query
If in the middle of query will require absolute match of email mostly
Expansion Operators
Statements without any parentheses or brackets are ANDed.
X Y ≡ X AND Y Statements inside parentheses or square brackets, () or are ANDed.
(X Y Z) ≡ X Y Z ≡ X && Y && Z
Statements inside curly brackets are ORed.
{X Y Z} ≡ X || Y || Z
The pipe symbol | is equivalent to the OR operator.
X | Y ≡ X OR Y The exclamation point ! is equivalent to the - operator.
X | Y ≡ X OR Y
Expansion
Filter statements and queries will expand. Expansion happens with expansion operators. Note that operator labels cannot be expanded. For example, you can’t search the to and from fields by doing {to: from:}someone@gmail.com. You can do to:someone@gmail.com OR from:someone@gmail.com.
Boolean Operators
Officially supported by gmail:
* OR
* -
* ” ”
Other boolean Operators
* AND
* | ≡ OR
* ! ≡ -
Operator Labels
It is important to understand that each of the operators that act directly on emails (ie those that search some part of an email directly) simply isolate that part of an email and search it with your query. If the query matches that field then it will be included in your filter, unless negated by some other operator.
Main Operators
from: Searches the From: field of emails
to: Searches the To: field of emails
subject: Searches the Subject: field of emails
list: Searches the To: && From: field of emails
cc: Searches the Cc: field of emails
bcc: Searches the Bcc: field of emails
Potentially Useful Operators
deliveredto: Searches the Delivered-To: field of emails
label: Searches within a certain label you have created
has:attachment Searches emails with attachments
filename: Searches emails with attachment of this type
is:chat Searches gchat history
Time Operators
Format date as yyyy/mm/dd
after: Searches messages after a certain date
before: Searches messages before a certain date
In Operators
in:anywhere Searches all emails including trash and spam
in:inbox Searches emails in your inbox
in:trash Searches emails in your trash
Is Operators
is:important Searches emails marked important by Priority Inbox
is:starred Searches starred emails
is:unread Searches unread emails
in:read Searches read emails
Has Star Operators
has: Search emails with a type of star
Stars include yellow-star, red-star, orange-star, green-star, blue-star, purple-star, red-bang, orange-guillemet, yellow-bang, green-check, blue-info, purple-question.
Each of the searches in these fields adhere to the searching behavior as describedhere. They will not search in the trash or spam folders.
Queries and Matching
Content that queries match against are expanded into words. Words are created by breaking the content at everything except letters, numbers, ampersands, and underscores. For example, someone@gmail.com is expanded into words equivalent to the list someone, gmail, com.
Filter Statements
There are two parts to every Gmail filter statement. The first part is the operator label (eg from:, to:, subject:). The second is the query. Filter statements are of the form operatorLabel:query.
Filter Maker
The filter maker is also known as the advanced search box. The drop-down arrow on the right side of the search box in Gmail will summon the filter maker.
From the filter maker you can use any field to describe what you want to filter. However, the most universal field, and the field that I recommend using exclusively is the Has the words field. This field is a direct proxy to Gmail’s powerful filtering mechanism.
At this point I recommend using a text editor to create, and save, your filters as you build them. (eg TextEdit, Notepad, TextMate, vim, emacs)
Sources
How Gmail Filter Email-Matching Works
Gmail::Using advanced search
Gmail::Boolean operators