如何检查字符串是否与perl中的正则表达式匹配?

发布于 2024-11-27 10:57:30 字数 1752 浏览 1 评论 0原文

我对 Perl 有疑问。我想解析电子邮件对象或日志或文件等。我想知道邮件是从哪里来的。首先我必须检查“x-envelop-from”行,如果不匹配,那么我必须检查“from”行。

这是我的一些示例文件:

X-Envelope-From: 
    <[email protected]>
From: "=?iso-8859-9?B?RXYgVGH+/W1hY/1s/fD9bmRhIsdfyhjdbmRRmltIFNlem9u?=
    =?iso-8859-9?B?dQ==?=" <[email protected]>

我的代码为此文件打印 2 行:

[email protected]
[email protected]

锄头是可能的,两条打印行都在 if 和 elsif 中打印?检查匹配是否有问题?

while ( $line = <FILE>) 
{
    my ($from, $to, $spam_id, $date, $tmp_date, $m_day, $m_mon, $m_year, $m_hour, $m_min, $pos_tmp);
    my ($subject);
# 
    if ( $line =~ m/^(X-Envelope-From:).*/ ) {
        if ( $line =~ m/^X-Envelope-From:.*<(.*)>.*/ ) {
            $from = $1;
        }
        else {
            $line = <FILE>;
            if ( $line =~ m/.*<(.*)>.*/ ) {
                $from = $1;
            }
        }
        print $from . "\n";
    }

    elsif ( $line =~ m/^(From:).*/ ) {
        if ( $line =~ m/^From:.*<(.*)>.*/ ) {
            $from = $1;
        }
        else {
            $line = <FILE>;
            if ( $line =~ m/.*<(.*)>.*/ ) {
                $from = $1;
            }
        }
        print $from . "\n";
    }
}

提前致谢。

i have a problem with perl. i want to parse an email object or log or file whatever. i want to find where is mail comes from. first i have to check "x-envelop-from" line, if there isn't match, then i have to check "from" line.

this is some of my sample file:

X-Envelope-From: 
    <[email protected]>
From: "=?iso-8859-9?B?RXYgVGH+/W1hY/1s/fD9bmRhIsdfyhjdbmRRmltIFNlem9u?=
    =?iso-8859-9?B?dQ==?=" <[email protected]>

my code prints 2 lines for this file:

[email protected]
[email protected]

hoe can be possible, both print lines are printed in if and elsif? is there a problem at checking matches?

while ( $line = <FILE>) 
{
    my ($from, $to, $spam_id, $date, $tmp_date, $m_day, $m_mon, $m_year, $m_hour, $m_min, $pos_tmp);
    my ($subject);
# 
    if ( $line =~ m/^(X-Envelope-From:).*/ ) {
        if ( $line =~ m/^X-Envelope-From:.*<(.*)>.*/ ) {
            $from = $1;
        }
        else {
            $line = <FILE>;
            if ( $line =~ m/.*<(.*)>.*/ ) {
                $from = $1;
            }
        }
        print $from . "\n";
    }

    elsif ( $line =~ m/^(From:).*/ ) {
        if ( $line =~ m/^From:.*<(.*)>.*/ ) {
            $from = $1;
        }
        else {
            $line = <FILE>;
            if ( $line =~ m/.*<(.*)>.*/ ) {
                $from = $1;
            }
        }
        print $from . "\n";
    }
}

thanks in advance.

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

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

发布评论

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

评论(1

┊风居住的梦幻卍 2024-12-04 10:57:31

使用专用模块,例如 Email::MIME解析标头:

#!/usr/bin/env perl

use strict;
use warnings;

use Email::MIME;

my $em = Email::MIME->new(
    do { local $/; <DATA> }
);

my $from = $em->header('X-Envelope-From');
$from = $em->header('From') unless $from;

$from =~ s{.*<|>.*}{}g;
print $from;

__DATA__
X-Envelope-From: 
    <[email protected]>
From: "=?iso-8859-9?B?RXYgVGH+/W1hY/1s/fD9bmRhIsdfyhjdbmRRmltIFNlem9u?=
    =?iso-8859-9?B?dQ==?=" <[email protected]>

Use a specialised module such as Email::MIME to parse the headers:

#!/usr/bin/env perl

use strict;
use warnings;

use Email::MIME;

my $em = Email::MIME->new(
    do { local $/; <DATA> }
);

my $from = $em->header('X-Envelope-From');
$from = $em->header('From') unless $from;

$from =~ s{.*<|>.*}{}g;
print $from;

__DATA__
X-Envelope-From: 
    <[email protected]>
From: "=?iso-8859-9?B?RXYgVGH+/W1hY/1s/fD9bmRhIsdfyhjdbmRRmltIFNlem9u?=
    =?iso-8859-9?B?dQ==?=" <[email protected]>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文