Last active
February 15, 2022 21:17
-
-
Save pmarreck/4626713 to your computer and use it in GitHub Desktop.
RFC 5322 Email Validation Regex in Ruby Regular Expressions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# RFC 5322 Email Validation Regex in Ruby | |
# This work is released under the BSD 3-Clause License | |
# http://choosealicense.com/licenses/bsd-3-clause/ | |
# Copyleft (ɔ) 2013, Peter R. Marreck | |
# All rights reserved. | |
# | |
# Redistribution and use in source and binary forms, with or without modification, | |
# are permitted provided that the following conditions are met: | |
# | |
# Redistributions of source code must retain the above copyright notice, this | |
# list of conditions and the following disclaimer. | |
# | |
# Redistributions in binary form must reproduce the above copyright notice, this | |
# list of conditions and the following disclaimer in the documentation and/or | |
# other materials provided with the distribution. | |
# | |
# Neither the name of the {organization} nor the names of its | |
# contributors may be used to endorse or promote products derived from | |
# this software without specific prior written permission. | |
# | |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR | |
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | |
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
EMAIL = / | |
(?<addr_spec> (?> \g<local_part> @ \g<domain> ) ){0} | |
(?<local_part> (?> \g<dot_atom> | \g<quoted_string> | \g<obs_local_part> ) ){0} | |
(?<domain> (?> \g<dot_atom> | \g<domain_literal> | \g<obs_domain> ) ){0} | |
(?<domain_literal> (?> \g<CFWS>? \[ (?: \g<FWS>? \g<dtext> )* \g<FWS>? \] \g<CFWS>? ) ){0} | |
(?<dtext> (?> [\x21-\x5a] | [\x5e-\x7e] | \g<obs_dtext> ) ){0} | |
(?<quoted_pair> (?> \\ (?: \g<VCHAR> | \g<WSP> ) | \g<obs_qp> ) ){0} | |
(?<dot_atom> (?> \g<CFWS>? \g<dot_atom_text> \g<CFWS>? ) ){0} | |
(?<dot_atom_text> (?> \g<atext> (?: \. \g<atext> )* ) ){0} | |
(?<atext> (?> [a-zA-Z0-9!\#\$%&'*\+\/\=\?\^_`{\|}~\-]+ ) ){0} | |
(?<atom> (?> \g<CFWS>? \g<atext> \g<CFWS>? ) ){0} | |
(?<word> (?> \g<atom> | \g<quoted_string> ) ){0} | |
(?<quoted_string> (?> \g<CFWS>? " (?: \g<FWS>? \g<qcontent> )* \g<FWS>? " \g<CFWS>? ) ){0} | |
(?<qcontent> (?> \g<qtext> | \g<quoted_pair> ) ){0} | |
(?<qtext> (?> \x21 | [\x23-\x5b] | [\x5d-\x7e] | \g<obs_qtext> ) ){0} | |
# comments and whitespace | |
(?<FWS> (?> (?: \g<WSP>* \r\n )? \g<WSP>+ | \g<obs_FWS> ) ){0} | |
(?<CFWS> (?> (?: \g<FWS>? \g<comment> )+ \g<FWS>? | \g<FWS> ) ){0} | |
(?<comment> (?> \( (?: \g<FWS>? \g<ccontent> )* \g<FWS>? \) ) ){0} | |
(?<ccontent> (?>\g<ctext> | \g<quoted_pair> | \g<comment> ) ){0} | |
(?<ctext> (?> [\x21-\x27] | [\x2a-\x5b] | [\x5d-\x7e] | \g<obs_ctext> ) ){0} | |
# obsolete tokens | |
(?<obs_domain> (?> \g<atom> (?: \. \g<atom> )* ) ){0} | |
(?<obs_local_part> (?> \g<word> (?: \. \g<word> )* ) ){0} | |
(?<obs_dtext> (?> \g<obs_NO_WS_CTL> | \g<quoted_pair> ) ){0} | |
(?<obs_qp> (?> \\ (?: \x00 | \g<obs_NO_WS_CTL> | \n | \r ) ) ){0} | |
(?<obs_FWS> (?> \g<WSP>+ (?: \r\n \g<WSP>+ )* ) ){0} | |
(?<obs_ctext> (?> \g<obs_NO_WS_CTL> ) ){0} | |
(?<obs_qtext> (?> \g<obs_NO_WS_CTL> ) ){0} | |
(?<obs_NO_WS_CTL> (?> [\x01-\x08] | \x0b | \x0c | [\x0e-\x1f] | \x7f ) ){0} | |
# character class definitions | |
(?<VCHAR> (?> [\x21-\x7E] ) ){0} | |
(?<WSP> [ \t] ){0} | |
\g<addr_spec> | |
/uix |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Email validation is basically the proof that getting 95% of the way there (wherever "there" is) is not hard, but getting 100% of the way there is an order of magnitude (or more) harder 😊