RFC822: email address validation with regular expressions

后端 未结 5 435
醉酒成梦
醉酒成梦 2020-12-07 03:40

as you know, this is how we validate an email address:

(?:(?:\\r\\n)?[ \\t])*(?:(?:(?:[^()<>@,;:\\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t]
)+|\         


        
5条回答
  •  情歌与酒
    2020-12-07 04:36

    If you want to understand what is going on, you should look at a decent module such as Email::Address and note how the pattern is built from its constituent pieces:

    my $CTL            = q{\x00-\x1F\x7F};
    my $special        = q{()<>\\[\\]:;@\\\\,."};
    
    my $text           = qr/[^\x0A\x0D]/;
    
    my $quoted_pair    = qr/\\$text/;
    
    my $ctext          = qr/(?>[^()\\]+)/;
    my ($ccontent, $comment) = (q{})x2;
    for (1 .. $COMMENT_NEST_LEVEL) {
      $ccontent = qr/$ctext|$quoted_pair|$comment/;
      $comment  = qr/\s*\((?:\s*$ccontent)*\s*\)\s*/;
    }
    my $cfws           = qr/$comment|\s+/;
    
    my $atext          = qq/[^$CTL$special\\s]/;
    my $atom           = qr/$cfws*$atext+$cfws*/;
    my $dot_atom_text  = qr/$atext+(?:\.$atext+)*/;
    my $dot_atom       = qr/$cfws*$dot_atom_text$cfws*/;
    
    my $qtext          = qr/[^\\"]/;
    my $qcontent       = qr/$qtext|$quoted_pair/;
    my $quoted_string  = qr/$cfws*"$qcontent+"$cfws*/;
    
    my $word           = qr/$atom|$quoted_string/;
    

    etc etc etc.

    my $simple_word    = qr/$atom|\.|\s*"$qcontent+"\s*/;
    my $obs_phrase     = qr/$simple_word+/;
    
    my $phrase         = qr/$obs_phrase|(?:$word+)/;
    
    my $local_part     = qr/$dot_atom|$quoted_string/;
    my $dtext          = qr/[^\[\]\\]/;
    my $dcontent       = qr/$dtext|$quoted_pair/;
    my $domain_literal = qr/$cfws*\[(?:\s*$dcontent)*\s*\]$cfws*/;
    my $domain         = qr/$dot_atom|$domain_literal/;
    

提交回复
热议问题