Regular Expression: comma-delimited email addresses

I have been using this RegEx for email address validation and it is working very well:

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
- a delimiter missing
- 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!
Who is Participating?
Theoretically you could do something like:

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

1) I am not sure how well 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).
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)

if (count($wrong_emails) > 0)
    echo "Wrong emails encountered: ".join(" ",$wrong_emails)."\n";
    // do something with $good_emails
ddeteringAuthor Commented:
Hi gnudiff,

Thank you for your reply! Well, I am using VB in 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?
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Note also that it might be possible to optimize your Regexp perhaps:


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: ?
ddeteringAuthor Commented:
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 ;)
ddeteringAuthor Commented:
doesn't let anything through - at least not with JavaScript from 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.
ddeteringAuthor Commented:
I got this one to work for five digit postal codes:
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...
There are many valid email addresses that do not match
ddeteringAuthor Commented:
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?
ddeteringAuthor Commented:
This here seems to work well:

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.
>> 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*|$))+


This works when I enter 2 emails addresses, but with just one, it does not.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.