How to match a comma separated list of emails with regex?

This Regex will allow emails with spaces after the commas.

^[\W]*([\w+\-.%]+@[\w\-.]+\.[A-Za-z]{2,4}[\W]*,{1}[\W]*)*([\w+\-.%]+@[\w\-.]+\.[A-Za-z]{2,4})[\W]*$

Playing around with this, a colleague came up with this RegEx that's more accurate. The above answer seems to let through an email address list where the first element is not an email address. Here's the update which also allows spaces after the commas.


Try this:

^([\w+-.%]+@[\w-.]+\.[A-Za-z]{2,4},?)+$

Adding the + after the parentheses means that the preceding group can be present 1 or more times.

Adding the ^ and $ means that anything between the start of the string and the start of the match (or the end of the match and the end of the string) causes the validation to fail.

Tags:

C#

Asp.Net

Regex