3

In my ~/.procmailrc I have the following lines:

:0
\* ^Subject:.*(O|o)pen( \|)(S|s)(onic|urge)
$MAILDIR/OpenSurge/

As you see, I would like to put any mail containing “Open Surge” (or any variant with space or capital letters) in the folder OpenSurge. But the messages containing this words only went to the default folder.

Notice that I have a lot of others recipes with ^From:foo and when I try ^Subject:.*OpenSurge my recipe work with “OpenSurge” but I doesn’t work with the regexp ^Subject:.*(O|o)pen( \|)(S|s)(onic|urge).

So, how can I match please with a regexp all possibilities of writing “Open Surge”?

1
  • The reason this doesn't work at all are the spurious backslashes. You have one in front of the leading * which is a syntax error, and ( \|) only matches the literal string consisting of one space followed by a literal pipe character. Anyway, the idiomatic way to write an optional space is a space followed by a question mark; the ? quantifier means "zero or one of the preceding expression".
    – tripleee
    Mar 3, 2014 at 16:56

3 Answers 3

1

How about this:

:0
* ^Subject:.*Open[ ]*Surge
OpenSurge

The default settings for procmail is to ignore case. See also D.

A matching message is stored in the file OpenSurge. It is normally located in ~/Mail/.

[Edit]

If you additionally want to match "Open sonic", you can use the regexp from @Bruce Ediger, but there are also other ways:

Add another matching rule

:0
* ^Subject:.*Open *sonic
OpenSurge

or

:0
* ^Subject:.*Open *Surge | \
  ^Subject:.*Open *sonic
OpenSurge

or

:0
* ^Subject:.*(Open *Surge|Open *sonic)
OpenSurge

and so on.

You also should not use the default .procmailrc but one with comments like this one.

To check what's going on switch on VERBOSE. The logs are stored in LOGFILE .

3
  • Thank you bersh. your code work. But I would like to match also "Open sonic". That’s why I did `.*Open[ ]*S(onic|surge). So Procmail doesn’t match any of the tow possibilities. I realised that Procmail have a problem with the (foo|bar) syntax. Other recipes with this syntax didn’t work. What is the mater? All documentations tell me I can use regex with Procmail :/
    – fauve
    Feb 26, 2014 at 5:18
  • Thank you, the following syntax work: :0 * ^Subject:.*Open *Surge | \ ^Subject:.*Open *sonic OpenSurge But I still have a question. Why are * ^Subject:.*(O|o)pen[ ](S|s)(onic|urge) didn’t work?
    – fauve
    Feb 27, 2014 at 2:17
  • I have no idea, what your header was. Somethin like this works for me: (echo 'Subject: Open surge';echo 'Subject: Open Sonic')|egrep '^Subject:.*(O|o)pen[ ](S|s)(onic|urge)'
    – user55518
    Feb 27, 2014 at 10:05
1

I think you got the "zero or more spaces between 'open' and 'surge'" part wrong:

* ^Subject:.*(O|o)pen *(S|s)(onic|urge)

I have ' *' (space asterisk) between 'pen' and '(S' in the regexp, just in case the stackexchange font it hard to read.

1

All regex's are equal ...but some are more equal than others:-)

Procmail has its very own regex engine (it does not use one from a library) and regex flavor (which is _almost the same as PCRE. Procmail regex's are case INsensitive almost all the time (you can use the D flag to make them case sensitive ...sorta). There are subtle gotchas and bugs in most Procmail regex engines: one of them is writing (X|x) for upper and lower case where the regex is already case INsensitive causes the regex engine to go a little loopy and not work correctly. {Anyway, the best/conventional way to write such a regex --assuming it's case sensitive, which it's not here-- is [Xx] rather than (X|x).}

2
  • Ok but then, if I want to catch several variants like open sonic and open surge (no mater about the case sensitiveness), can I at least do open (sonic|surge)?
    – fauve
    Jan 23, 2020 at 11:29
  • @fauve - yes, the xxx (yyy|zzz) syntax is supported by the procmail regex flavor. IMHO you need to find (hello Dr. Google) a thorough and complete procmail regex manual, as otherwise you'll stumble around endlessly trying to figure out which constructs from other flavors of regex work and which don't. Jan 24, 2020 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.