Skip to content

Instantly share code, notes, and snippets.

@satob
Created June 30, 2020 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satob/0dc003aefe020d5f1e9263bd7e173f9f to your computer and use it in GitHub Desktop.
Save satob/0dc003aefe020d5f1e9263bd7e173f9f to your computer and use it in GitHub Desktop.
# http://www.din.or.jp/~ohzaki/mail_regex.htm#RFC
my $CR = qq{\\x0D};
my $LF = qq{\\x0A};
my $CRLF = qq{(?:\\x0D\\x0A)};
my $VCHAR = qq{[\\x21-\\x7E]};
my $WSP = qq{[\\x20\\x09]};
my $obs_NO_WS_CTL = qq{[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x7F]};
my $obs_ctext = $obs_NO_WS_CTL;
my $obs_qtext = $obs_NO_WS_CTL;
my $obs_qp = qq{(?:\\\\(?:\\x00|$obs_NO_WS_CTL|$LF|$CR))};
my $obs_FWS = qq{(?:$WSP+(?:$CRLF$WSP+)*)};
my $FWS = qq{(?:(?:$WSP*$CRLF)?$WSP+|$obs_FWS)};
my $ctext = qq{(?:[\\x21-\\x27\\x2A-\\x5B\\x5D-\\x7E]|$obs_ctext)};
my $quoted_pair = qq{(?:\\\\(?:$VCHAR|$WSP)|$obs_qp)};
my $ccontent = qq{(?:$ctext|$quoted_pair|(?-1))};
my $comment = qq{(\\((?:$FWS?$ccontent)*$FWS?\\))};
my $CFWS = qq{(?:(?:$FWS?$comment)+$FWS?|$FWS)};
my $atext = qq{[A-Za-z0-9!#\$%&'*+\\-/=?^_`{|}~]};
my $atom = qq{(?:$CFWS?$atext+$CFWS?)};
my $dot_atom_text = qq{(?:$atext+(?:\\.$atext+)*)};
my $dot_atom = qq{(?:$CFWS?$dot_atom_text$CFWS?)};
my $qtext = qq{(?:[\\x21\\x23-\\x5B\\x5D-\\x7E]|$obs_qtext)};
my $qcontent = qq{(?:$qtext|$quoted_pair)};
my $quoted_string = qq{(?:$CFWS?"(?:$FWS?$qcontent)*$FWS?"$CFWS?)};
my $obs_dtext = qq{(?:$obs_NO_WS_CTL|$quoted_pair)};
my $dtext = qq{(?:[\\x21-\\x5A\\x5E-\\x7E]|$obs_dtext)};
my $domain_literal = qq{(?:$CFWS?\\[(?:$FWS?$dtext)*$FWS?\\]$CFWS?)};
my $word = qq{(?:$atom|$quoted_string)};
my $obs_local_part = qq{(?:$word(?:\\.$word)*)};
my $obs_domain = qq{(?:$atom(?:\\.$atom)*)};
my $local_part = qq{(?:$dot_atom|$quoted_string|$obs_local_part)};
my $domain = qq{(?:$dot_atom|$domain_literal|$obs_domain)};
my $addr_spec = qq{$local_part\@$domain};
my $mail_regex = $addr_spec;
$email = q/abc.def@example.com/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/"abc.def"@example.com/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/"abc./."\n".q/ def"@example.com/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/"abc"."def"@example.com/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/'abc.def'@example.com/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/'abc./."\n".q/ def'@example.com/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/'foo'.'bar'@example.com/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/(abc)abc.def@example.com/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/(abc) abc.def@example.com/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/<abc.def@example.com>/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/<abc/."\n".q/ .def@example.com>/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/<abc"def"ghi@example.com>/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/foo bar <abc.def@example.com>/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/'foo bar' <abc.def@example.com>/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/"foo bar" <abc.def@example.com>/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/abc.def@[example.com]/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/abc.def@[exa mple.com]/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/abc.def@[exa/."\n".q/mple.com]/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/abc.def@[example."hoge".com]/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/,abc.def@example.com/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/;abc.def@example.com/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/abc..def@example.com/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/abc.def.@example.com/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/.abc.def@example.com/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/⛄bc.def@example.com/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/abc.def@localhost/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/abc.def@e.c/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/abc.def@e.co/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/abc.def@-.com/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/abc.def@203.0.113.1/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/ABC.DEF@example.com/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/"<script>" <abc.def@example.com>/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
$email = q/'<script>' <abc.def@example.com>/;
print $email if $email =~ /\A$mail_regex\z/o;
print "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment