gpt4 book ai didi

regex - 用于电子邮件地址的 perl 正则表达式

转载 作者:行者123 更新时间:2023-12-04 06:26:28 26 4
gpt4 key购买 nike

我在 exim 日志中为这行编写正则表达式时遇到了一些麻烦

 1. 2011-05-12 11:30:26 1QKRHt-0001aD-Vd => mail <mail@mail.example.com> F=<root@example.com> bla bla 
2. 2011-04-22 12:01:31 1QDCF0-0002ss-Nw => /var/mail/mail <root@mail.mealstrom.org.ua> F=<root@example.com> bla bla
3. 2011-05-12 11:29:01 1QKRGU-0001a5-Ok => mail@mail.example.com F=<root@example.com> bla bla

我想把这个变量 mail@mail.example.com 在一个正则表达式中。我尝试使用逻辑来解决这个问题:查找 'F=' 之前的最后一个字符串,由空格分隔并可以锁定在 < >

你能帮我写下这个逻辑吗?

最佳答案

您可以使用以下正则表达式:

# the line should be in variable $line
if ($line =~ /.*?\s+<?(\S+?)>?\s+F=/) {
# ...
}

然后用 Mail-RFC822-Address 验证你的匹配是个好主意。 perl 模块,所以完整的代码可能是:
use Mail::RFC822::Address qw(valid);

# the line should be in variable $line
if ($line =~ /.*?\s+<?(\S+?)>?\s+F=/) {
if (valid($1)) {
# ...
}
}

关于regex - 用于电子邮件地址的 perl 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5988294/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com