Searching GMail with Regex

This article was posted more than 1 year ago. Please keep in mind that the information on this page may be outdated, insecure, or just plain wrong today.

Interesting discussion in IT Professionals community I saw regarding needing to be able to search GMail for some IP addresses.
I’ve tinkered around with a few things and found a pretty easy way to do the regex searches.

Step 1: Setup Google Doc’s Sheet

Follow these directions to the end of #2 below:

  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 lable (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.

Step 2: Modify Script

We need to modify the script since the subject is what contains the IP address that have been emailed.
Click on Tools > Script editor…
When this opens, modify line #32:
Originally, it says:

var msg = messages[m].getBody();

Change it to:

var msg = messages[m].getSubject();

Save your script…


Back on the spreadsheet, fill in the search field with the following regex.


Note: This will match–999.999.999.999 — but who cares… we’re not being that strict are we? I’ll leave the exercise to you to filter for proper IPv4 address or even IPv6 hah.
So with the regex entered in, go up to GMail Regex > Search Mailbox

Results, Prosper

Here’s an example?—?I sent myself some test messages with some IP addresses in the subject.

Leave a Comment