How to extract email address from a string using p

2020-04-14 01:38发布

问题:

How to extract emails address from a string using perl and to put the email addres into a variable? My strings looks like

Ben Tailor <bentaylor@gmail.com>
barbara@gmail.com, barbara2@gmail.com, Ben Tailor <bentaylor@gmail.com>

I tryed this

$string ="Ben Tailor <bentaylor@gmail.com>";
$string =~ /\b([^\s]+@[^\s]+)\b/g ;
print $string;

And the Out put xas:

Ben Tailor <bentaylor@gmail.com>

Someone have an Idea?

Fixed using

Email::Valid->address($string);

Thx

回答1:

Start with https://metacpan.org/pod/Email::Valid. It seems to work pretty well.



回答2:

Take a look at Email::Address or Email::AddressParser from cpan

 my @addrs = Email::Address->parse(
    q[me@local, Tony <me@local>, "Tony" <me@local>]
  );

This returns a list of Email::Address objects it finds in the input string.



标签: regex perl email