【问题标题】:How to convert a regex from PCRE to POSIX format, that warns about repetition-operator operand invalid?如何将正则表达式从 PCRE 转换为 POSIX 格式,警告重复运算符操作数无效?
【发布时间】:2012-01-07 03:41:44
【问题描述】:

尝试匿名接收来自经过身份验证的 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 文件中使用?

【问题讨论】:

    标签: regex posix pcre postfix-mta


    【解决方案1】:

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

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

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

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

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

    【讨论】:

    • 谢谢@just_somebody 这有助于。经过测试可以正常工作的 POSIX regexp: for postfix 变成了/^Received: from (.* \([-._a-zA-Z0-9]+ \[[.0-9]{7,15}\]\))(.*[[:space:]]+)\(Authenticated sender: ([^)]+)\)(.*)by (host\.domain\.tld) \(([^)]+)\) with (E?SMTPS?A?) id ([A-F0-9]+)(.*\))/ REPLACE Received: from [127.0.0.1] (localhost [127.0.0.1])$2(Authenticated sender: $3)${4}by $5 ($6) with $7 id $8$9
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    相关资源
    最近更新 更多