3

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 <[email protected]>
[email protected], [email protected], Ben Tailor <[email protected]>

I tryed this

$string ="Ben Tailor <[email protected]>";
$string =~ /\b([^\s]+@[^\s]+)\b/g ;
print $string;

And the Out put xas:

Ben Tailor <[email protected]>

Someone have an Idea?

Fixed using

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

Thx

1
  • It would output the same as the input string because all you did was perform a regex match against it then output the string again. You didn't use the captured part $1 or substitue replace s///g; Commented Jul 18, 2010 at 16:44

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.