How to convert a regex from PCRE to POSIX format, that warns about repetition-operator operand invalid?

我与影子孤独终老i 提交于 2019-12-11 07:56:11

问题


Trying to anonymize received headers for relayed messages from authenticated postfix users, there is an example from https://we.riseup.net/debian/anonymizing-postfix:

/^Received: from (.* \([-._[:alnum:]]+ \[[.[:digit:]]{7,15}\]\)).*?([[:space:]]+).*\(Authenticated sender: ([^)]+)\).*by (auk\.riseup\.net) \(([^)]+)\) with (E?SMTPS?A?) id ([A-F[:digit:]]+).*/ REPLACE Received: from [127.0.0.1] (localhost [127.0.0.1])$2(Authenticated sender: $3)${2}with $6 id $7

When editing the file regexp:/etc/postfix/header_checks the result is an error message:

line 15: repetition-operator operand invalid

Now my guess is that the above regex is in PCRE format, where my Postfix requests a POSIX compatible regular expression.

How to make the above regular expression POSIX regexp compliant for use in a Postfix header_checks file?


回答1:


Your hunch is correct, .*? is a PCRE construct: .* is normal "any character, as many times as possible, at least zero times", and the trailing question mark changes that to "... as few times as possible ...". SUSv4 says:

The behavior of multiple adjacent duplication symbols ( '+' , '*' , '?' , and intervals) produces undefined results.

i haven't studied the pattern too much, but you should be able to work around this particular incompatibility: the next subpattern is ([[:space:]]+), so you should be able to reformulate it as "any non-space character...":

[^[:space:]]*([[:space:]]+)

or maybe just get rid of the problem by omitting the question mark. the space-eater is followed by another .* after all.



来源:https://stackoverflow.com/questions/8767172/how-to-convert-a-regex-from-pcre-to-posix-format-that-warns-about-repetition-op

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!