regular expression help [port]

Henrik Motakef henrik.motakef at web.de
Sun Sep 15 06:45:05 EDT 2002


":B nerdy" <thoa0025 at mail.usyd.edu.au> writes:

> Greetings, this is a regular expression for an email address
> "^[a-z0-9_]+@[a-z0-9\-]{2,}\.[a-z0-9\-\.]{2,}$"

No, it's not. The format of email addresses is a lot more complicated
than this. Just for a start, you don't allow a dot before the @, so
my address wouldn't match.

J. Friedl, author of "Mastering Regular Expressions", wrote a perl
script that generates a regular expression to check email addresses
for syntactical validity, see
<http://examples.oreilly.com/regex/readme.html>. Is is huge and
ugly. Additionaly, it is almost useless, because a regex cannot tell
you if an email address really exists, which is most often what you
want to know.

The only sane way to make sure that an email address really exists is
to send an email to it and to wait for a reply.

> i wish to valid a variable against this string. would someone be so kind to
> supply a few lines of code to do this?

>>> import re
>>> pattern = re.compile("^[a-z0-9_]+@[a-z0-9\-]{2,}\.[a-z0-9\-\.]{2,}$")
>>> pattern.match('":B nerdy" <thoa0025 at mail.usyd.edu.au>')
>>> pattern.match('thoa0025 at mail.usyd.edu.au')
<_sre.SRE_Match object at 0x8679f40>

Regards
Henrik



More information about the Python-list mailing list