128

We receive regular automated build messages from Jenkins build servers at work.

It'd be nice to ferret these away into a label, skipping the inbox.

Using a filter is of course the right choice.

The desired identifier is the string [RELEASE] at the beginning of a subject line.

Attempting to specify any of the following regexes causes emails with the string release in any case anywhere in the subject line to be matched:

\[RELEASE\]*
^\[RELEASE\]
^\[RELEASE\]*
^\[RELEASE\].*

From what I've read subsequently, Gmail doesn't have standard regex support, and from experimentation it seems, as with google search, special characters are simply ignored.

I'm therefore looking for a search parameter which can be used, maybe something like atstart:mystring in keeping with their has:, in: notations.

Is there a way to force the match only if it occurs at the start of the line, and only in the case where square brackets are included?

Sincere thanks.

KomodoDave
  • 7,239
  • 10
  • 60
  • 92
  • Shouldn't `^\[RELEASE\].*` do the trick or do I misunderstand what you want to do? – Hauns TM Sep 03 '12 at 11:06
  • No regardless of escapes that regex format doesn't work. From what I've read gmail doesn't have standar regex support, and from experimentation it seems, as with google search, special characters are simply ignored. I'm looking for a "secret" flag or such which can be used' maybe something like `atstart:mystring` in keeping with their `has:`, `in:` notations. I'll add this info into my post for clarification, thank you anyway. – KomodoDave Sep 03 '12 at 11:09

4 Answers4

157

Regex is not on the list of search features, and it was on (more or less, as Better message search functionality (i.e. Wildcard and partial word search)) the list of pre-canned feature requests, so the answer is "you cannot do this via the Gmail web UI" :-(

There are no current Labs features which offer this. SIEVE filters would be another way to do this, that too was not supported, there seems to no longer be any definitive statement on SIEVE support in the Gmail help.

Updated for link rot The pre-canned list of feature requests was, er canned, the original is on archive.org dated 2012, now you just get redirected to a dumbed down page telling you how to give feedback. Lack of SIEVE support was covered in answer 78761 Does Gmail support all IMAP features?, since some time in 2015 that answer silently redirects to the answer about IMAP client configuration, archive.org has a copy dated 2014.

With the current search facility brackets of any form () {} [] are used for grouping, they have no observable effect if there's just one term within. Using (aaa|bbb) and [aaa|bbb] are equivalent and will both find words aaa or bbb. Most other punctuation characters, including \, are treated as a space or a word-separator, + - : and " do have special meaning though, see the help.

As of 2016, only the form "{term1 term2}" is documented for this, and is equivalent to the search "term1 OR term2".

You can do regex searches on your mailbox (within limits) programmatically via Google docs: http://www.labnol.org/internet/advanced-gmail-search/21623/ has source showing how it can be done (copy the document, then Tools > Script Editor to get the complete source).

You could also do this via IMAP as described here: Python IMAP search for partial subject and script something to move messages to different folder. The IMAP SEARCH verb only supports substrings, not regex (Gmail search is further limited to complete words, not substrings), further processing of the matches to apply a regex would be needed.

For completeness, one last workaround is: Gmail supports plus addressing, if you can change the destination address to youraddress+jenkinsrelease@gmail.com it will still be sent to your mailbox where you can filter by recipient address. Make sure to filter using the full email address to:youraddress+jenkinsrelease@gmail.com. This is of course more or less the same thing as setting up a dedicated Gmail address for this purpose :-)

mr.spuratic
  • 9,767
  • 3
  • 34
  • 24
  • Is there an escape character to search for brackets or punctuation? – Aron Sep 07 '18 at 11:41
  • @Aron I don't believe so. Although some punctuation is handled specially (code-related, like `c++`) it's best to think of search as working on pre-indexed words only. – mr.spuratic Sep 20 '18 at 11:20
  • I think you can use double quote " to escape brackets. I'm using in my filters and looks like it works. If I use `subject: {[term1] (part of term2}` I find all the messages containing both _term1_ and _part of term2_ in the subject. But with the form `subject:{"[term1] (part of term2"}` I find all the messages with subjects starting exactly with _[term1] (part of term2_ – Izerlotti Jan 03 '20 at 07:55
  • I get the feeling that plus addressing is how Google is primarily expecting us to get the most control out of filters, given that they also call them [task-specific email addresses](https://support.google.com/a/users/answer/9308648?hl=en) – Spencer Williams Mar 31 '22 at 18:39
4

Using Google Apps Script, you can use this function to filter email threads by a given regex:

function processInboxEmailSubjects() {
  var threads = GmailApp.getInboxThreads();
  for (var i = 0; i < threads.length; i++) {
    var subject = threads[i].getFirstMessageSubject();
    const regex = /^\[RELEASE\]/; //change this to whatever regex you want, this one should cover OP's scenario
    let isAtLeast40 = regex.test(subject)
    if (isAtLeast40) {
      Logger.log(subject);

      // Now do what you want to do with the email thread. For example, skip inbox and add an already existing label, like so:
      threads[i].moveToArchive().addLabel("customLabel")
    }
  }
}

As far as I know, unfortunately there isn't a way to trigger this with every new incoming email, so you have to create a time trigger like so (feel free to change it to whatever interval you think best):

function createTrigger(){ //you only need to run this once, then the trigger executes the function every hour in perpetuity
  ScriptApp.newTrigger('processInboxEmailSubjects').timeBased().everyHours(1).create();
} 
jlo
  • 2,157
  • 2
  • 17
  • 23
1

The only option I have found to do this is find some exact wording and put that under the "Has the words" option. Its not the best option, but it works.

stock
  • 117
  • 5
-10

I was wondering how to do this myself; it seems Gmail has since silently implemented this feature. I created the following filter:

Matches: subject:([test])
Do this: Skip Inbox

And then I sent a message with the subject

[test] foo

And the message was archived! So it seems all that is necessary is to create a filter for the subject prefix you wish to handle.

  • 9
    Note that if you send a message with the subject 'test foo', your filter will also catch it. "[test]" only really searches for "test" =) – Daniel Gill Feb 28 '14 at 21:04
  • 35
    Here's a tip: when you want to test an idea, try to come up with ways to disprove it. In your case, you wrote a filter to capture the square brackets, and your test expected the filter to work. You should test it expecting it to fail, like @DanielGill pointed out. :) – pedromanoel Apr 17 '14 at 17:56
  • 1
    In addition to (incorrectly) matching subject "test foo" as noted, this filter also matches "foo test", i.e. with no regard to the "start of line" spec. But your enthusiasm is there, mrfitzy, and that ought to count for something. – Ezekiel Victor Sep 25 '22 at 15:27