Searching GMail with Regex

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

Source: http://www.labnol.org/internet/advanced-gmail-search/21623/
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…
1
When this opens, modify line #32:
Originally, it says:

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

Change it to:

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

2
Save your script…
3

Search!

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

d{1,3}.d{1,3}.d{1,3}.d{1,3}

Note: This will match 0.0.0.0–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
4

Results, Prosper

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

Leave a Reply

Your email address will not be published. Required fields are marked *