A few weeks back, while working on an application, I came upon a common problem…I required a regular expression (REGEX) to match emails. Certainly, there were a number of examples I could borrow from across the internet, but I had two cases I had to deal with in addition to the standard (general) match:
- Emails that were “hidden” i.e [email protected]…ite.com
- Addresses that were “non-emaily” emails i.e [email protected]png
According to the RFC 5322,
Addresses occur in several message header fields to indicate senders
and recipients of messages. An address may either be an individual
mailbox, or a group of mailboxes.
Normally, a mailbox is composed of two parts: (1) an optional display
name that indicates the name of the recipient (which can be a person
or a system) that could be displayed to the user of a mail
application, and (2) an addr-spec address enclosed in angle brackets
After a number of trial and errors…and the goodness of http://www.regexr.com/ , I came up with the following
You can see my REGEX in action here: http://regexr.com/3bh7j
Enjoy and don’t hesitate to provide feedback!