如何将正则表达式从 PCRE 转换为 POSIX 格式,以警告重复运算符操作数无效?

发布于 2024-12-25 09:42:05 字数 755 浏览 4 评论 0原文

尝试对来自经过身份验证的 postfix 用户的中继消息的收到标头进行匿名化,有一个来自 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

编辑文件 regexp:/etc/postfix/header_checks 时,结果是一条错误消息:

第 15 行: 重复操作符操作数无效

现在我的猜测是上面的正则表达式是 PCRE 格式,我的 Postfix 请求 POSIX 兼容的正则表达式。

如何使上述正则表达式 POSIX regexp 兼容,以便在 Postfix header_checks 文件中使用?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心在旅行 2025-01-01 09:42:05

您的预感是正确的, .*? 是 PCRE 构造: .* 是正常的“任何字符,尽可能多次,至少零次”,并且尾随问号将其更改为“...尽可能少的次数...”。 SUSv4 说:

多个相邻重复符号(“+”、“*”、“?”和间隔)的行为会产生未定义的结果。

我没有过多研究该模式,但您应该能够解决这种特定的不兼容性:下一个子模式是 ([[:space:]]+),因此您应该能够将其重新表述为“任何非空格字符...”:

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

或者可以通过省略问号来解决问题。毕竟,太空吞噬者后面跟着另一个 .*

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文