Skip to content

Instantly share code, notes, and snippets.

@dungsaga
Last active January 15, 2023 00:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dungsaga/394327ede90d57c82e5d4a8b8b20a22a to your computer and use it in GitHub Desktop.
Save dungsaga/394327ede90d57c82e5d4a8b8b20a22a to your computer and use it in GitHub Desktop.
practical email regex

This is my attempt to improve on the regex that match 99.99% of email from https://www.regular-expressions.info/email.html

This is a practical implementation of RFC 5322 where I omitted IP addresses, domain-specific addresses, the syntax using double quotes and square brackets.

  • Maximum length of email address is 254
  • Maximum length of the local part is 64
  • The local part must not contain double dots and must not start or end with a dot.
  • Maximum length of each domain label is 63
  • The domain part must contain at least 2 domain labels, each domain label must not start or end with a hyphen. Domain labels are separated by 1 dot.
  • The top level domain must contain only alphabet letters and must contain at least 2 letters.

email regex in JS:

/^(?!.{255}|\.)([a-z0-9!#$%&'*+\/=?^_`{|}~-]|\.(?!\.|@)){1,64}@(\b[a-z0-9-]{1,63}\b\.)+[a-z]{2,63}$/i

email regex in PHP:

'/^(?!.{255}|\.)([a-z0-9!#$%&\'*+\/=?^_`{|}~-]|\.(?!\.|@)){1,64}@(\b[a-z0-9-]{1,63}\b\.)+[a-z]{2,63}$/i'

email regex in Python:

r"(?i)^(?!.{255}|\.)([a-z0-9!#$%&'*+/=?^_`{|}~-]|\.(?!\.|@)){1,64}@(\b[a-z0-9-]{1,63}\b\.)+[a-z]{2,63}$"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment