Email Validator RE

Mark Schoonover schoon at amgt.com
Thu Nov 15 16:59:50 CST 2001


~sdpm~
Ken,

	Check out the Owl book - Mastering RE form O'reilly. They have your
answer in appendix B... FYI - it's 6,598 char long!

.mark

-----Original Message-----
From: Ken Loomis [mailto:KLoomis at bigplanet.com]
Sent: Thursday, November 15, 2001 2:03 PM
To: san-diego-pm-list at happyfunball.pm.org
Subject: Email Validator RE


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



~sdpm~

The posting address is: san-diego-pm-list at hfb.pm.org

List requests should be sent to: majordomo at hfb.pm.org

If you ever want to remove yourself from this mailing list,
you can send mail to <majordomo at happyfunball.pm.org> with the following
command in the body of your email message:

    unsubscribe san-diego-pm-list

If you ever need to get in contact with the owner of the list,
(if you have trouble unsubscribing, or have questions about the
list itself) send email to <owner-san-diego-pm-list at happyfunball.pm.org> .
This is the general rule for most mailing lists when you need
to contact a human.




More information about the San-Diego-pm mailing list