我正在從服務器獲取一封電子郵件并嘗試從陣列中匹配它。
#!/usr/bin/perl
@array = qw/will steve frank john/;
$match = “Steve <[email protected]>"; # has to be full name and email
if (grep /$match/, @array) {
print "found it\n";
}else{
print "no match\n";
}
exit
提前致謝
uj5u.com熱心網友回復:
這將匹配 will 和 steve:
您的方法是在陣列中查找全文“Steve [email protected]”,但它不存在。
#!/usr/bin/perl
my @array = qw/will steve frank john/;
my $match = 'Steve <[email protected]>'; # has to be full name and email
if ( my @found = grep { $match =~ /$_/ } @array ) {
# it's there
print "Match: \n\t@found\n";
}
輸出:
Match:
will steve
uj5u.com熱心網友回復:
在 Perl 中可能有無數種方法可以做到這一點。我會使用正則運算式將電子郵件從您嘗試匹配的內容中剔除,因為您的串列只是名稱。然后使用 lc 將名稱($match)小寫,因為您的串列都是小寫的。如果您碰巧需要 $match 保持不變,您可以使用哈希來跟蹤所有內容。在示例電子郵件中,我必須在 @ 前面使用反斜杠,但我認為在您的實際程式中,您無需擔心這一點。
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
my @array = qw/will steve frank john/;
my $match = "Steve <stevewilliams\@email.com>"; # has to be full name and email
# regex to trim name from email \s gets the whitespace if any.
$match =~ s/\s?\<.*$//g;
#lower case it
$match = lc($match);
print "'$match'\n";
if (grep /$match/, @array) {
print "found it\n";
}else{
print "no match\n";
}
exit;
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/409715.html
標籤:
上一篇:Perl腳本不喜歡日期擴展
