procmail
[Top] [All Lists]

Re: procmail regex style using Or construct

2009-02-19 19:28:07
At 22:06 2009-02-19 +0100, Ruud H.G. van Tol wrote:
Professional Software Engineering wrote:

 * MATCH ?? ^^(1|2|3|4|5|6|7)^^

Alternative:

   * MATCH ?? ^^[1-7]^^

NOT a good way to do it for the overall strategy needed by the OP - as I indicated in another reply on this thread, weeks 2, 3, 4, and 5 can't use the same strategy, and the character class RANGE is very likely to be misinterpreted by someone as meaning they can use 8-14, 15-21, etc. It's a whole lot more readable and less error prone if each of the conditions follows the same structure:

 * MATCH ?? ^^(1|2|3|4|5|6|7)^^

 * MATCH ?? ^^(8|9|10|11|12|13|14)^^

 * MATCH ?? ^^(15|16|17|18|19|20|21)^^

 * MATCH ?? ^^(22|23|24|25|26|27|28)^^

 * MATCH ?? ^^(29|30|31)^^

It is straightfoward, and anyone should have an easy time reading and understanding it.

There is little to be gained from convoluting it in an attempt for regexp brevity (which may not even result in any performance optimization):

        * MATCH ?? ^^[1-7]^^

        * MATCH ?? ^^([8-9]|1[0-4])^^

        * MATCH ?? ^^(1[5-9]|2[0-1])^^

        * MATCH ?? ^^2[2-8]^^

        * MATCH ?? ^^(29|3[0-1])^^

Really, the straightforward approach is more readable and far less prone to introduction of an error because someone can't read it easily.


An alternative is to use scoring - let procmail track the numerics AS numerics. It complicates the recipe while at the same time freeing you from trying to match specific date values. It isn't the way to deal with this case, but in the interest of explanation, I'll provide a functioning recipe demonstrating an approach:


# Date format modified here based on test messages I had available to me.
# Use whatever your MUA is generating if you're using sent mail, and be
# prepared for a wide range of funky formats if dealing with received
# mail - extracting the date from the from_ header should be a LOT more
# reliable in terms of format.

:0
* $ ^Date:[     ]*.*[   ]*\/[0-9]+ $MNTH $YR
* MATCH ?? ^\/[0-9]+
{
        # $MATCH contains the day of the month, which should be 1-31
        # negate it, then start working our way forward one week at a
        # time.  The initial value is adjusted by +1 in order to shift
        # the negated day number to base 0 (and all following checks will
        # be based on the already adjusted day value)
        :0
        * 1^0
        * $ -$MATCH^0
        * 7^0
        {
                # we're positive - so first week
                WEEK=1
        }

        # use score left over from prior recipe.
        :0E
        * $ $=^0
        * 7^0
        {
                #we're positive - so SECOND week
                WEEK=2
        }

        # use score left over from prior recipe.
        :0E
        * $ $=^0
        * 7^0
        {
                #we're positive - so THIRD week
                WEEK=3
        }

        # use score left over from prior recipe.
        :0E
        * $ $=^0
        * 7^0
        {
                #we're positive - so FOURTH week
                WEEK=4
        }

        # use score left over from prior recipe.
        :0E
        * $ $=^0
        * 7^0
        {
                #we're positive - so FIFTH week
                WEEK=5
        }

        # arguably, we shouldn't ever get HERE
        :0E
        {
                WEEK=HOSED
        }

        # file the message
        :0:
        mailbox.$YR.$MNTH.$WEEK.mbx
}



Using 'bc' or some shell trickery, you could accomplish math as well. There's also the possibility of using grep to match a day number to a filename to use.

---
 Sean B. Straw / Professional Software Engineering

 Procmail disclaimer: <http://www.professional.org/procmail/disclaimer.html>
 Please DO NOT carbon me on list replies.  I'll get my copy from the list.

____________________________________________________________
procmail mailing list   Procmail homepage: http://www.procmail.org/
procmail(_at_)lists(_dot_)RWTH-Aachen(_dot_)DE
http://MailMan.RWTH-Aachen.DE/mailman/listinfo/procmail