简体   繁体   English

主题标题的后缀正则表达式更改

[英]postfix regex change for subject headers

I am using the below regex to change the subject on mails sent through postfix on 我正在使用下面的正则表达式来更改通过postfix发送的邮件的主题

/etc/postfix/header_checks / etc / postfix / header_checks

/^Subject: System command Run on (.*)-(.*)-(.*)\.(.*)/ REPLACE Subject: System command Run on $4 $2$3
/^Subject: System command Run on (.*)-(.*)-(.*)\.(.*)\.(.*)/ REPLACE Subject: System command Run on $5 $2$3

which means any mail coming from this host 这意味着来自该主机的任何邮件

server-env-01.app

will be caught by the first filter and subject will be changed 将被第一个过滤器捕获,主题将被更改

and hostname with this format 和此格式的主机名

server-prod-01.loc.dc1

will be caught by the second filter 将被第二个过滤器捕获

However the second filter is not being used cause the first filter is also applied for the hostname with this format server-prod-01.loc.dc1 但是,没有使用第二个过滤器,因为第一个过滤器也以这种格式server-prod-01.loc.dc1应用于主机名

any suggestions??? 有什么建议么???

Your problem is that .* matches every character as often as possible and overshoots your intended sub-match. 您的问题是.* 尽可能匹配每个字符并且超出了您想要的子匹配。 In fact, every .* goes right to the end of the string, "giving back" characters only as far as it must to allow the rest of the expression to complete. 实际上,每个.*都将直接到达字符串的末尾,仅在允许表达式其余部分完成的范围内“回馈”字符。 That's called greedy matching and is the default behavior of regex engines. 这称为贪婪匹配,是正则表达式引擎的默认行为。

Therefore it's crucial that you are specific when using the * . 因此,在使用*时务必要具体。

Specific for the individual segments of "server-prod-01.loc.dc1" would be whitelists (like [a-z0-9]* or \\w* ) or a blacklist [^\\s.-]* . 对于"server-prod-01.loc.dc1"的各个段,特定的是白名单(例如[a-z0-9]*\\w* )或黑名单[^\\s.-]*

In your case I suppose that \\w* would suffice. 在您的情况下,我想\\w*就足够了。 It neither matches - nor . 它既不匹配-也不匹配. , so it can't overshoot. ,因此它不能超调。

^Subject: System command Run on (\w*)-(\w*)-(\w*)\.(\w*)

and

^Subject: System command Run on (\w*)-(\w*)-(\w*)\.(\w*)\.(\w*)

The alternative would be to use a non-greedy quantifier ( *? , or better +? , because * also matches zero characters): 另一种选择是使用非贪婪的量词( *?或更好的+?因为*还匹配零个字符):

^Subject: System command Run on (.+?)-(.+?)-(.+?)\.(.+?)

Though a) not all engines support non-greedy matching and b) being as specific as possible has a number of advantages... a regex that more clearly states its intention being one of them. 尽管a)并非所有引擎都支持非贪婪匹配,并且b)尽可能具体地具有许多优点...正则表达式更清楚地说明了其意图是其中之一。


Read about repetition and greedyness . 阅读有关重复和贪婪的内容

the below worked fine...though I had some inputs from Tomalak response above. 下面的工作很好...尽管我从上面的Tomalak响应中得到了一些建议。

if /^Subject:/
/^Subject: System command run on \b([^.-]*)-([^.-]*)-([^.-]*)\.([^.-]*)\b$/ REPLACE Subject: System command run on $4 $2$3
/^Subject: System command run on \b([^.-]*)-([^.-]*)-([^.-]*)\.([^.-]*)\.([^.-]*)$/ REPLACE Subject: System command run on $5 $2$3
endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM