Use Regular Expressions to Find Anything in your Gmail Account

Gmail offers some very useful search commands to help you quickly find messages that could be buried deep in your mailbox. For instance, a query like from:(John OR David) has:attachment after:2021/01/15 will locate all emails received in the past six month that have attachments and the send is either John or David.

The built-in Gmail search is powerful but there are some scenarios where it will not work. For instance, you cannot perform a wildcard search inside Gmail (like apples \* red). Or how do you locate all emails that have a phone number mentioned in the message body? It is also not possible to perform case-sensitive searches inside Gmail.

You can however do all this  in Gmail with the help of regular expressions (or regex).

Gmail Search with Regular Expressions

Advanced Gmail Search with Regular Expressions

Regular expressions allow you to perform complex search operations in most popular programming languages and the same can be used with Gmail as well using Google Docs. Let’s start with a simple example.

Say you are looking for email messages that contain any variation of the string “gray color”. There could be one or more spaces between the two words, the words may use either British or American spelling and the first letters may or may not be capitalized. The regular expression thus will be:


Where do you put this search expression? The search box in Gmail doesn’t support regular expressions directly but with Google Docs, it can. Here’s how:

  1. Click here to make a copy of the Gmail RegEx sheet into your Google Docs account.
  2. Wait for 10-15 seconds and a new Gmail RegEx menu will appear in your new Google sheet. Choose Initialize from the menu and grant the necessary permissions as requested by the program.
  3. The program will search your entire mailbox by default but if you would like to limit the search to any particular label (say Inbox or Spam), just put that label name in cell F3.
  4. Now enter any regular expression in the cell F4 and choose “Search Mailbox” from the Gmail RegEx menu to begin searching.

Any messages that match your search criteria will be listed in the initial columns of the same sheet. If you would like to perform a new search, simply change the regex in cell F4 and choose Search from RegEx menu again. Please do note that Google Apps Script allows you to perform 10,000 read operations in Gmail per day.

And if you are curious to know how the program works, here’s the annotated source code.

Amit Agarwal

Amit Agarwal

Google Developer Expert, Google Cloud Champion

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India.

Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory


Awards & Titles

Digital Inspiration has won several awards since it's launch in 2004.

Google Developer Expert

Google Developer Expert

Google awarded us the Google Developer Expert award recogizing our work in Google Workspace.

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards in 2017.

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional (MVP) title for 5 years in a row.

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator title recognizing our technical skill and expertise.

Email Newsletter

Sign up for our email newsletter to stay up to date.

We will never send any spam emails. Promise.