Email Validator RE

Bob Kleemann rkleeman at energoncube.net
Thu Nov 15 16:56:06 CST 2001


~sdpm~
Your current pattern will not allow matches to something like
"person at demon.co.uk".

There is a monster pattern match at the end of the O'Reilly book
"Mastering Regular Expressions" (the owl book).  That's alwyas been the
definitive one for me. 

http://www.oreilly.com/catalog/regex/
http://examples.oreilly.com/regex/readme.html

But if you wanted something reasonable, try this:

^[^\W_][\w.-]*\@([^\W_]+([.-]?[^\W_]+)*)+\.[^\W_]{2,4}$

Or the commented variety:

Which would allow email addr's from .uk, .com, and .info through.

BTW, I don't believe an underscore is allowed in an email address, and
it's definitely not allowed in a domain.

On Thu, 15 Nov 2001, Ken Loomis wrote:

> 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