Email Validator RE
Ken Loomis
KLoomis at bigplanet.com
Thu Nov 15 16:02:42 CST 2001
Once again this week I was called in as an emergency substitute for a
Perl class. Though I am no expert on Regular Expressions, I decided to
tackle it.
As a learning exercise I had the class develop an RE to validate email
addresses. Not too original I guess, but at least they could all
understand the need for doing that.
We started with this as the simplest test:
if ($email =~ /.*\@.*/) {
print "$email is a good email address !\n";
else {
print "$email is NOT a good email address !\n";
}
Then we repeatedly tested this with an invalid email address and if our
routine certified it as a good address, we added or modified the RE to
catch that case.
We ended up with this:
if ($email =~
/^[a-zA-Z0-9][a-zA-Z0-9_.-]*@([a-zA-Z0-9][a-zA-Z0-9_-]*[a-zA-Z0-9]|[a-zA-Z0-9][[a-zA-Z0-9_-]*\.[a-zA-Z0-9]*]*[a-zA-Z0-9]|[a-zA-Z0-9])\.\w\w\w$/)
{
print "$email is a good email address !\n";
else {
print "$email is NOT a good email address !\n";
}
I told them that even though it seemed to work in all the cases we could
think of, there was probably a more elegant way to do it. We looked on
the Internet and found a few suggested examples, but none seemed to be a
robust as what we had created.
It seemed our biggest challenge was to allow an email that looked like
this:
myname at financial.yahoo.com
but not validate:
myname at financial..yahoo.com
We figured out a way to do it using extra non-RE tests, but since this
was a lesson in RE's, we wanted to see if there were a way to do it all
with one RE.
I told them that I would ask this email list for help or suggestions. If
anyone can offer suggestions we would greatly appreciate your input.
Thanks,
Ken Loomis
-------------- next part --------------
A non-text attachment was scrubbed...
Name: KLoomis.vcf
Type: text/x-vcard
Size: 435 bytes
Desc: Card for Ken Loomis
Url : http://mail.pm.org/archives/san-diego-pm/attachments/20011115/ec1b3978/KLoomis.vcf
More information about the San-Diego-pm
mailing list