Skip to content

Instantly share code, notes, and snippets.

@gregseth
Last active May 1, 2023 15:31
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gregseth/5582254 to your computer and use it in GitHub Desktop.
Save gregseth/5582254 to your computer and use it in GitHub Desktop.
RegEx for RFC 2822 compliant email address.

Complete version

(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\ x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

Simpler version

[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?

Sources

@GregoryMaks
Copy link

There's a space that breaks complete regex version - [\ x01

@sinneren
Copy link

Error in link RegEx email.htlm -> email.html

@danielturus
Copy link

The complete version doesn't work. You can copy and try yourself.

@sonyarianto
Copy link

The complete version I try on regex101.com and I need to escape the / char

/[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/g

now works :)

@kuriel-trivu
Copy link

kuriel-trivu commented Jul 21, 2022

@sonyarianto you don't have to modify anything, just choose the right option:

image

@ZiiSolutions
Copy link

@kuriel-trivu
Where can I get access to those different flavours?
You got a url?

@MreeP
Copy link

MreeP commented Oct 15, 2022

@michael-freidgeim-webjet

To support upper-case in the email regex a-z should be replaced with a-zA-Z

@MNF
Copy link

MNF commented Dec 2, 2022

The referred article https://www.regular-expressions.info/email.html suggests

This regex is intended to be used with your regex engine’s “case insensitive” option turned on

It should be mentioned at the top of this page

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