Solved

Regular Expression: comma-delimited email addresses

Posted on 2004-09-02
12
1,907 Views
Last Modified: 2008-02-01
Hi!
I have been using this RegEx for email address validation and it is working very well:
^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$

Now, I would like to validate comma-delimited input of multiple email addresses into a textbox in order to throw an error message when there is
- a wrong email address in there
or
- a delimiter missing
or
- a wrong delimiter being used.

It should accept anything that has strings matching the RegEx above and delimited by spaces (doesn't matter how many, could be none) and ONE comma. Any ideas? Thanks!
0
Comment
Question by:ddetering
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 3

Expert Comment

by:gnudiff
ID: 11971204
Well, the solution would depend on the programming language you use, which you haven't indicated. ;)

Still, it would be easy for program to detect a wrong email address, but much harder to see if there is a delimiter missing or a "wrong" delimiter is being used - that would require some analysis and multiple regexp checks - do you really need that?

The program in PHP would look something like this:

Assuming $input variable contains the textbox entry;

$single_email_regexp = '....';// your email regexp, not included for brevity's sake

$email_list = preg_split(("/[\s,]+/",$input);
$wrong_emails = array();
$good_emails = array();

foreach ($email_list as $email)
      if ( preg_match($single_email_regexp, $email) != 1)
                 $wrong_emails[]=$email;
       else
              $good_emails[]=$email;

if (count($wrong_emails) > 0)
{
    echo "Wrong emails encountered: ".join(" ",$wrong_emails)."\n";
}
else
    // do something with $good_emails
0
 
LVL 1

Author Comment

by:ddetering
ID: 11973071
Hi gnudiff,

Thank you for your reply! Well, I am using VB in ASP.net and your approach would work there just fine. I thought I could do the entire job with a properly defined RegEx so that it would run in the JavaScript validator, too... I imagine some sort of grouping where my RegEx becomes just one element in another RegEx that allows only MyRegEx separated by exactly one comma and any number of spaces (or none). Wouldn't that work?
0
 
LVL 3

Accepted Solution

by:
gnudiff earned 250 total points
ID: 11975148
Possibly.
Theoretically you could do something like:

((your 1 email regexp)(\s*,\s*))+

However:
1) I am not sure how well ASP.net or client-side JS handle recursive pattern matches;
2) This by itself still does not give answer to what exactly was wrong (wrong delim/missing delim).
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 3

Expert Comment

by:gnudiff
ID: 11975264
Note also that it might be possible to optimize your Regexp perhaps:

^(([[:alnum:]]+[_\-\.\+]+))*[[:alnum:]]+@(\w+[\.\-]+)*\w{1,63}\.[a-zA-Z]{2,6}$

I haven't tested, but I think this should do the same yours does (the [:alnum:] is a shortcut for A-Za-z0-9, should work with PCRE - Perl compatible regexps).

Also, does your regexp handle things with domain count greater than 3, for example:
email@lab.dept.division.agency.gov ?
0
 
LVL 1

Author Comment

by:ddetering
ID: 11975538
gnudiff, thanks a lot for your comments. Yes, the regex does handle extra long domain names, and I will also try your optimization. I will, of course, try your suggested regex for recursive pattern matching and will let you know shortly whether it works. I don't have a problem with an unspecific error message, you can't expect everything ;)
0
 
LVL 1

Author Comment

by:ddetering
ID: 11976036
Unfortunately,
((^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$)(\s*,\s*))+
doesn't let anything through - at least not with JavaScript from ASP.net. Maybe the ^ and $ indicators are irritating? I couldn't submit anything past it.

Another question: I assume this one would, if it worked, require a comma at the end of the string. Any way around it?

It has gotten more complicated than I thought and I will increase the points.
0
 
LVL 1

Author Comment

by:ddetering
ID: 11984485
I got this one to work for five digit postal codes:
(((D-)?\d{5})(\s*,*\s*))+
It does not require a comma at the end anymore but lets through a string like this as well:
12345 12345

This one: (((D-)?\d{5})(\s*,\s*))+
allows only a string like this 12345, 12345,
So I would have to make my users add a comma at the end of their string.

Hence, we now know that recursive matching is not a problem. Two problems remain:
1) My RegEx for an email address has to be integrated into gnudiff's form to work as well as the zip code example above.
2) Gnudiff's recursive pattern matching form has to be optimized to allow a string that does not end with a comma but requires commas as a delimiter.

Maybe after Labor Day...
0
 
LVL 84

Expert Comment

by:ozo
ID: 11985400
There are many valid email addresses that do not match
(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}
0
 
LVL 1

Author Comment

by:ddetering
ID: 11985563
even though it is off-topic - for example? I have just replaced another one with this because it caused problems... Any ideas on my question?
0
 
LVL 1

Author Comment

by:ddetering
ID: 11991144
This here seems to work well:
(((([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6})(\s*,\s*))+(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$

As you can see, I have removed the start character ^ from my email expression and simply added the email expression again after the recursive expression indicating with $ that this should be the end of the expression. It was easier than expected after gnudiff's help.
0
 
LVL 3

Expert Comment

by:gnudiff
ID: 11995710
>> 2) Gnudiff's recursive pattern matching form has to be optimized to allow a string that does not end with a comma but requires commas as a delimiter.

>> (your 1 email regexp)(\s*,\s*))+

I believe you could possibly simply add the end of line match:

(your 1 email regexp)(\s*,\s*|$))+

0
 

Expert Comment

by:etsellinc
ID: 24214885
(((([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6})(\s*,\s*))+(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$

This works when I enter 2 emails addresses, but with just one, it does not.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question