Skip to content

Instantly share code, notes, and snippets.

@dmkc
Created June 17, 2011 17:42
Show Gist options
  • Save dmkc/1031883 to your computer and use it in GitHub Desktop.
Save dmkc/1031883 to your computer and use it in GitHub Desktop.
A mostly RFC822 compatible email address regex that captures just the email adress. Accepts addresses like "Jimi Hendrix" <jimi@virgin.com>, John Lennon <john@ilikecatcherintherye.com>, "<cobain@shotguns.com>" <cobain@shotguns.com> and "anything &@#^" <va
(?:(?:\"[^\"]+\"|[^@,\"]+)\\s+<(?=[^>]+>))?([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4})(?:>)?
@dmkc
Copy link
Author

dmkc commented Jun 17, 2011

This will parse an email like "hello@mail.com>" as valid, but if capture group 1 is used all should be OK regardless. Alternatively, the email part could be copy and pasted to split the regex into two cases, one with the 'bare' email and one with the name part in front and the email surrounded by angled brackets, but for my current purposes I'm only using whatever is in group 1 and thus don't care. Regex in a lookbehind won't work for most regex engines and I can't think of another way off the top of my head.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment