Email Validator RE

Ken Loomis KLoomis at
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 =~

                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

    myname at

but not validate:

    myname at

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.

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 :

More information about the San-Diego-pm mailing list