Sieve: filter subject with regex, file into mailbox named match

dovecotemail-servergrepregexsieve

I am trying to filter mails by subject with a regular expression.

The subjects I want to match are read like [git-foo] some more text where foo is the string I want to check for.

I end up with the following

require ["fileinto", "variables", "regex"];

if header :regex "subject" "^\[git-.*\]" {
    set :lower :upperfirst "repository" "${1}";

    if string :is "${repository}" "" {
        fileinto "Test/default";
    } else {
        fileinto "Test/${repository}";
    }
}

Replacing first if statement with if header :matches "subject" "[git-*" { files mails into Test/Foo] some more text but, when correcting "[git-*" to "[git-*]", mails do not match.

Regular expression works with grep -e.

What to do to file mail correctly into Test/Foo?

Best Answer

You need to combine both capture groups and double backlashes. Both were mentioned in previous answers, but separately.

if header :regex "subject" "^\\[git-(.*)\\]" {
  set :lower :upperfirst "repository" "${1}";
  // ...
}

Explanation:

  • unlike :matches, :regex only sets match variables ($1, etc.) for the capture group. :matches sets them for each wildcard.

  • :regex does indeed require escaping [ and ], but with a double backslash.