Email Validator RE

The FAQchest faqchest at
Sat Nov 17 01:11:39 CST 2001

Ken, here is an excerpt of a script doing at one time some mail notification.

I've adopted a paranoid approach each time I have to deal with system or
apps call using an open () command. I enclose the whole routine in an
eval{}. If the mail server (MTA) has whatsoever troubles (most of the
time network/DNS) and crahes, the calling script doesn't die, just the eval.

In more secured scripts, I use an alarm signal to include some timeout
control on this IO.

You see there are a certain numer of regexp to make sure tha e-mail
addresses are ok.


sub clearspaces {
  return (0) unless (my ($instr) = @_);
  $instr =~ s,^[ \t\f]+,,;
  $instr =~ s,[ \t\f\r]+\n$,,;
  $instr =~ s,[ \t\f]+, ,g;
  return $instr;

sub mail_notification {
  print "Trying to mail the commit message ...\n";
  return (0) unless (my @text = @_);

  my $emregexp = '([\w-_.]+)@(([\w-_]+[.])+[a-zA-Z]+[ ,]?)';
  my $subject = &clearspaces ("cvs commit: $ARGV[0]");
  my $mailto = &clearspaces ($MAIL_TO);
  $mailto =~ s/[\s]+/,/g;
  $mailto =~ s,\\,,g;
  $mailto =~ tr/A-Z/a-z/;
  return (0) unless ($mailto =~ /^($emregexp)+$/);
  print " ... Done mailing the message ...\n";

  eval {  # don't let the main apps crashing on this IO
    open (MAIL, "| $MLISTAPPL -s \"$subject\" $mailto") || return (0);
    print (MAIL join("\n", @text));
    close (MAIL);

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
> 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.
> Thanks,
> Ken Loomis

The posting address is: san-diego-pm-list at

List requests should be sent to: majordomo at

If you ever want to remove yourself from this mailing list,
you can send mail to <majordomo at> 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> .
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