From: demerphq Date: Fri, 27 Feb 2009 09:25:12 GMT Subject: Re: Checking for a valid email address Message-ID: <9b18b3110902270125p76ac118ckd21cb2d427940db2@mail.gmail.com> In-Reply-To: <9b18b3110902270120t15babbc0oe2975c086297e1e0@mail.gmail.com> 2009/2/27 demerphq : > 2009/2/27 Adam Mercer : >> Hi >> >> We are in the process of migrating some of our repositories from CVS >> to Git in a two step process, first to a centralised Git setup then >> (hopefully) onto a more distributed development model. In the testing >> we have been doing so far the only problem we have run into is >> developers not setting their email addresses correctly so the >> changelogs are filled with spurious address. Does anyone know of a >> pre-commit hook that could check for a valid email address prior to >> allowing a commit? Or another way that this can be acheived? > > If you are using Perl 5.10 or later then you can use the following regex: > > my $email = qr { >    (?(DEFINE) >      (?
        (?&mailbox) | (?&group)) >      (?         (?&name_addr) | (?&addr_spec)) >      (?       (?&display_name)? (?&angle_addr)) >      (?      (?&CFWS)? < (?&addr_spec) > (?&CFWS)?) >      (?           (?&display_name) : (?:(?&mailbox_list) | (?&CFWS))? ; >                                             (?&CFWS)?) >      (?    (?&phrase)) >      (?    (?&mailbox) (?: , (?&mailbox))*) > >      (?       (?&local_part) \@ (?&domain)) >      (?      (?&dot_atom) | (?"ed_string)) >      (?          (?&dot_atom) | (?&domain_literal)) >      (?  (?&CFWS)? \[ (?: (?&FWS)? (?&dcontent))* (?&FWS)? >                                    \] (?&CFWS)?) >      (?        (?&dtext) | (?"ed_pair)) >      (?           (?&NO_WS_CTL) | [\x21-\x5a\x5e-\x7e]) > >      (?           (?&ALPHA) | (?&DIGIT) | [!#\$%&'*+-/=?^_`{|}~]) >      (?            (?&CFWS)? (?&atext)+ (?&CFWS)?) >      (?        (?&CFWS)? (?&dot_atom_text) (?&CFWS)?) >      (?   (?&atext)+ (?: \. (?&atext)+)*) > >      (?            [\x01-\x09\x0b\x0c\x0e-\x7f]) >      (?     \\ (?&text)) > >      (?           (?&NO_WS_CTL) | [\x21\x23-\x5b\x5d-\x7e]) >      (?        (?&qtext) | (?"ed_pair)) >      (?   (?&CFWS)? (?&DQUOTE) (?:(?&FWS)? (?&qcontent))* >                           (?&FWS)? (?&DQUOTE) (?&CFWS)?) > >      (?            (?&atom) | (?"ed_string)) >      (?          (?&word)+) > >      # Folding white space >      (?             (?: (?&WSP)* (?&CRLF))? (?&WSP)+) >      (?           (?&NO_WS_CTL) | [\x21-\x27\x2a-\x5b\x5d-\x7e]) >      (?        (?&ctext) | (?"ed_pair) | (?&comment)) >      (?         \( (?: (?&FWS)? (?&ccontent))* (?&FWS)? \) ) >      (?            (?: (?&FWS)? (?&comment))* >                          (?: (?:(?&FWS)? (?&comment)) | (?&FWS))) > >      # No whitespace control >      (?       [\x01-\x08\x0b\x0c\x0e-\x1f\x7f]) > >      (?           [A-Za-z]) >      (?           [0-9]) >      (?            \x0d \x0a) >      (?          ") >      (?             [\x20\x09]) >    ) > >    (?&address) > }x; > > which is a fully RFC compliant perl regex for validating any email address. Er, i meant validating whether any email address is well formed of course. Cheers, yves -- perl -Mre=debug -e "/just|another|perl|hacker/"