Re: [exim] catching newlines with ${sg {}{}{}}

Etusivu
Poista viesti
Vastaa
Lähettäjä: Phil Pennock
Päiväys:  
Vastaanottaja: Marten Lehmann
Kopio: exim-users
Aihe: Re: [exim] catching newlines with ${sg {}{}{}}
On 2008-06-17 at 20:53 +0200, Marten Lehmann wrote:
> I need to extract the value "X-purgate-ID" from $spam_report:
>
> X-purgate: Spam
>     X-purgate-ID: 150741::080616223818-6C9786C0-73CE72D8/2129941411-0/0-3
>     X-purgate-Ad: For more information about eXpurgate please visit 
> http://www.expurgate.net/

>
> With real PCRE, the expression would look like this:


Exim uses "real" PCRE; Philip Hazel is the original author of both.

> .*\nX-purgate-ID: (.*?)\n.*
>
> whereas $1 would contain the id. Unfortunately, the sg expansion item
> does seem to work with newlines.


If you double-check the documentation on ${sg ...} then you'll see the
reminder:

----------------------------8< cut here >8------------------------------
                           Because all three arguments are expanded before use,
    if any $ or \ characters are required in the regular expression or in the
    substitution string, they have to be escaped. For example:


    ${sg{abcdef}{^(...)(...)\$}{\$2\$1}}


    yields "defabc", and


    ${sg{1=A 4=D 3=C}{\N(\d+)=\N}{K\$1=}}


    yields "K1=A K4=D K3=C". Note the use of "\N" to protect the contents of
    the regular expression from string expansion.
----------------------------8< cut here >8------------------------------


Try:
${sg{$spam_report}{\N^.*\n\s*X-purgate-ID: (.*?)\n.*$\N}{\$1}}

Note the \s* to match the whitespace you showed above, the \N at each
end of the <regex> field and the \$1, so that $1 would be expanded by
the regex engine, instead of expanded as an Exim variable passed in to
be used in the substitution pattern.

That is, it's perfectly fine to use $acl_m_foo as the substitution, Exim
expanded that for you; so to use $1 for a regexp, you pass \$1.

Regards,
-Phil