views:

41

answers:

3

I have a RegEx for validating email addresses, but I'm really looking to validate a whole From header. Any of these would be valid:

[email protected]
<[email protected]>
My Name <[email protected]>

Is there anything out there that would validate these as valid from headers? I'm going to look in the smtp library :)

A: 
r'\w*<?[\w\-][A-Za-z0-9\-\.\+%]+@[\w\-][\w\-\.]+[a-zA-Z]{1,4}>?'
Zonda333
Doesn't handle e.g. `[email protected]`
David Zaslavsky
+1  A: 

Be aware that there are plenty of other valid cases of e-mail addresses beyond what you've posted.

See here for a recipe that may help. Also read this for a great discussion of parsing email addresses with a regex. There are any number of good regexes in there that will match the uses you're looking for, imho :-)

godswearhats
A: 

I couldn't get the posted response to work, so I've been working on this and finally got this one, which seems to work so far. I'm sure it'll miss/catch something, but it's working for now.

[a-zA-Z0-9+_\-\.\ ]*[ ]*<?[a-zA-Z0-9+_\-\.]+@[0-9a-zA-Z][.-0-9a-zA-Z]*.[a-zA-Z]+>?
Mark Kecko