Gmail filter with regular expressions

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 = /- r\d.* -/; //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:

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();
} 

Read more here: Source link