如何检查字符串是否与perl中的正则表达式匹配?
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用专用模块,例如
Email::MIME
解析标头:Use a specialised module such as
Email::MIME
to parse the headers: