Однажды в момент безумия я написал эту подпрограмму Perl на основе книги «Освоение регулярных выражений»:
sub getRFC822AddressSpec
{
my ($esc, $space, $tab, $period) = ('\\\\', '\040', '\t', '\.');
my ($lBr, $rBr, $lPa, $rPa) = ('\[', '\]', '\(', '\)');
my ($nonAscii, $ctrl, $CRlist) = ('\200-\377', '\000-\037', '\n\015');
my $qtext = qq{ [^$esc$nonAscii$CRlist] }; # within "..."
my $dtext = qq{ [^$esc$nonAscii$CRlist$lBr$rBr] }; # within [...]
my $ctext = qq{ [^$esc$nonAscii$CRlist()] }; # within (...)
my $quoted_pair = qq{ $esc [^$nonAscii] }; # an escaped char
my $atom_char = qq{ [^()$space<>\@,;:".$esc$lBr$rBr$ctrl$nonAscii] };
my $atom = qq{ $atom_char+ # some atom chars
(?!$atom_char) # NOT followed by part of an atom
};
# rfc822 comments are (enclosed (in parentheses) like this)
my $cNested = qq{ $lPa (?: $ctext | $quoted_pair )* $rPa };
my $comment = qq{ $lPa (?: $ctext | $quoted_pair | $cNested )* $rPa };
# whitespace and comments may be scattered liberally
my $X = qq{ (?: [$space$tab] | $comment )* };
my $quoted_str = qq{ " (?: $qtext | $quoted_pair )* " };
my $word = qq{ (?: $atom | $quoted_str ) };
my $domain_ref = $atom;
my $domain_lit = qq{ $lBr (?: $dtext | $quoted_pair )* $rBr };
my $sub_domain = qq{ (?: $domain_ref | $domain_lit ) };
my $domain = qq{ $sub_domain (?: $X $period $X $sub_domain )* };
my $local_part = qq{ $word (?: $X $period $X $word )* };
my $addr_spec = qq{ $local_part $X \@ $X $domain };
# return a regular expression object
return qr{$addr_spec}ox;
}
my $spec = getRFC822AddressSpec();
my $address = q{foo (Mr. John Foo) @ bar. example};
print "$address is an email address" if ($address =~ qr{$spec});