var q listofhotmailemailaddressesdownloadmoviesUsing a regular expression to validate an email address. Quick answer. Use the following regex for input validation 9A Z. 9 A Z tt 0 9. A Za z0 9. A Za z 0,6. A Za z. 0 9. A Za z0 9. A Za z 0,6. A Za zAddresses matched by this regex have a local part i. RFC 5. 32. 15. 32. The second constraint is a restriction on RFC 5.
Elaborate answer. Using a regular expression that recognizes email addresses could be useful in various situations for example to scan for email addresses in a document, to validate user input, or as an integrity constraint on a data repository. It should however be noted that if you want to find out if the address actually refers to an existing mailbox, theres no substitute for sending a message to the address. If you only want to check if an address is grammatically correct then you could use a regular expression, but note that is a grammatically correct email address that certainly doesnt refer to an existing mailbox. The syntax of email addresses has been defined in various RFCs, most notably RFC 8. Email how it is supposed to be Free, simple and secure Manage multiple mail accounts in one place, from any device Sign up today Gmail is an easy to use email app that saves you time and keeps your messages safe. Get your messages instantly via push notifications, read and respond. Replace your annoying Did you know factoids with even more annoying Actually thats fake corrections. Wikipedia features a massive list of common. RFC 5. 32. 2. RFC 8. RFC 5. 32. 2 as the latest standard. The syntax defined in RFC 8. In this answer Ill take email address to mean addr spec as defined in the RFCs i. John Doelt jdoeexample. Theres one problem with translating the RFC syntaxes into regexes the syntaxes are not regularTo set up a block address list, logon to your Yahoo Mail account and click on the Options link under the Search button. Then select Mail Options. This is because they allow for optional comments in email addresses that can be infinitely nested, while infinite nesting cant be described by a regular expression. To scan for or validate addresses containing comments you need a parser or more powerful expressions. Note that languages like Perl have constructs to describe context free grammars in a regex like way. In this answer Ill disregard comments and only consider proper regular expressions. The RFCs define syntaxes for email messages, not for email addresses as such. Addresses may appear in various header fields and this is where they are primarily defined. When they appear in header fields addresses may contain between lexical tokens whitespace, comments and even linebreaks. Semantically this has no significance however. By removing this whitespace, etc. Thus, the canonical representation of first. Different syntaxes should be used for different purposes. If you want to scan for email addresses in a possibly very old document it may be a good idea to use the syntax as defined in RFC 8. On the other hand, if you want to validate user input you may want to use the syntax as defined in RFC 5. You should decide which syntax applies to your specific case. I use POSIX extended regular expressions in this answer, assuming an ASCII compatible character set. RFC 8. 22. I arrived at the following regular expression. I invite everyone to try and break it. If you find any false positives or false negatives, please post them in a comment and Ill try to fix the expression as soon as possible. lt ,. Fx. 7Fnrrnrr. lt ,. Fx. 7Fnrrnrrlt ,. Fx. 7Fnrrnrr. lt ,. Fx. 7Fnrrnrr believe its fully complient with RFC 8. It only recognizes email addresses in their canonical form. For a regex that recognizes folding whitespace see the derivation below. The derivation shows how I arrived at the expression. I list all the relevant grammar rules from the RFC exactly as they appear, followed by the corresponding regex. Where an erratum has been published I give a separate expression for the corrected grammar rule marked erratum and use the updated version as a subexpression in subsequent regular expressions. As stated in paragraph 3. RFC 8. 22 optional linear white space may be inserted between lexical tokens. Where applicable Ive expanded the expressions to accommodate this rule and marked the result with opt lwsp. CHAR lt any ASCII character. CTL lt any ASCII control character and DEL. Fx. 7F. CR lt ASCII CR, carriage return. LF lt ASCII LF, linefeed. SPACE lt ASCII SP, space. HTAB lt ASCII HT, horizontal tab. ASCII quote mark. CRLF CR LF. rn. LWSP char SPACE HTAB. CRLF LWSP char. CHAR. qtext lt any CHAR excepting lt, CR, and including linear white space. CHAR excluding, CR, including linear white space. CHAR except specials, SPACE and CTLs. Fx. 7F. word atom quoted string. Fx. 7Fnrrnrrn tr. Fx. 7Fnrrnrrn tr. Fx. 7Fnrrnrrn tr. lt ,. Fx. 7Fnrrnrrn trFx. 7Fnrrnrrn trrn t. rn tlt ,. Fx. 7Fnrrnrrn tr Fx. 7Fnrrnrrn tr. lt ,. Fx. 7Fnrrnrrn trFx. 7Fnrrnrrn trrn t. rn tlt ,. Fx. 7Fnrrnrrn tr Fx. 7Fnrrnrrn tr. lt ,. Fx. 7Fnrrnrrn trlt ,. Fx. 7Fnrrnrrn tr. lt ,. Fx. 7Fnrrnrrn tr Fx. 7Fnrrnrrn trrn t. rn tlt ,. Fx. 7Fnrrnrrn trrn trn tlt ,. Fx. 7Fnrrnrrn trrn t. rn tlt ,. Fx. 7Fnrrnrrn tr Fx. 7Fnrrnrr. lt ,. Fx. 7Fnrrnrrlt ,. Fx. 7Fnrrnrr. lt ,. Fx. 7Fnrrnrr RFC 5. 32. 2I arrived at the following regular expression. I invite everyone to try and break it. If you find any false positives or false negatives, please post them in a comment and Ill try to fix the expression as soon as possible. 9 A Z. 9 A Z tt 9A Z. 9 A Z t Z I believe its fully complient with RFC 5. It only recognizes email addresses in their canonical form. For a regex that recognizes folding whitespace see the derivation below. The derivation shows how I arrived at the expression. I list all the relevant grammar rules from the RFC exactly as they appear, followed by the corresponding regex.