尝试匹配电子邮件中的名称
Trying to match a name inside an email
我正在从服务器抓取一封电子邮件并尝试从数组中匹配它。
#!/usr/bin/perl
@array = qw/will steve frank john/;
$match = “Steve <stevewilliams@email.com>"; # has to be full name and email
if (grep /$match/, @array) {
print "found it\n";
}else{
print "no match\n";
}
exit
提前致谢
在 Perl 中可能有无数种方法可以做到这一点。
由于您的列表只是名称,因此我会使用正则表达式 trim 您尝试匹配的电子邮件。
然后使用 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;
这将匹配 will 和 steve:
您的方法是在数组中查找全文“Steve stevewilliams@email.com”,但它不存在。
#!/usr/bin/perl
my @array = qw/will steve frank john/;
my $match = 'Steve <stevewilliams@email.com>'; # 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
我正在从服务器抓取一封电子邮件并尝试从数组中匹配它。
#!/usr/bin/perl
@array = qw/will steve frank john/;
$match = “Steve <stevewilliams@email.com>"; # has to be full name and email
if (grep /$match/, @array) {
print "found it\n";
}else{
print "no match\n";
}
exit
提前致谢
在 Perl 中可能有无数种方法可以做到这一点。 由于您的列表只是名称,因此我会使用正则表达式 trim 您尝试匹配的电子邮件。 然后使用 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;
这将匹配 will 和 steve:
您的方法是在数组中查找全文“Steve stevewilliams@email.com”,但它不存在。
#!/usr/bin/perl
my @array = qw/will steve frank john/;
my $match = 'Steve <stevewilliams@email.com>'; # 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