Home > database >  Trying to match a name inside an email
Trying to match a name inside an email

Time:01-12

I am grabbing an email from a server and trying to match it from an array.

#!/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

Thanks in advance

CodePudding user response:

This will match on will and steve:

Your method was looking for the full text "Steve [email protected]" in the array but it does not exist.

#!/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";
}

Output:

Match: 
    will steve

CodePudding user response:

There are probably a zillion ways to to this in Perl. I would use a regular expression to trim the email out of what your trying to match since your list is just names. Then use lc to lowecase the name ($match), since your list is all lower case. If you happen to need $match to remain unchanged you could use a hash to keep track of everything. I had to use a backslash in-front of the @ in the example email, but I think in your real program you wont need to worry about that.

#!/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;
  •  Tags:  
  • Related