Parse a small string for name and email?

I have a line:

John Smith <jsmith@gmail.com>

I would like to get two variables:

name (John Smith) and email address ( jsmith@gmail.com )

How can i do this?

Thanks for the help!

+5
source share
1 answer

There are more forms of a valid email address on the Internet than you probably understand. I would suggest using other code to analyze them, for example email.utils.parseaddr .

For example, a valid address is:

"Rocky J. Squirrel" <rocky.squirrel@gmail.com>

Here is the name Rocky J. Squirrel, not "Rocky J. Squirrel".

( < > ):

rocky.squirrel@gmail.com (Rocky J. Squirrel)

parens "", . ( Python email.utils.parseaddr.)

( ):

>>> import email.utils
>>> email.utils.parseaddr("John Smith <jsmith@gmail.com>")
('John Smith', 'jsmith@gmail.com')
+17

All Articles