Regular expression for the multiple email address

I have a regular expression like below:

^[A-Za-z0-9.|-|_]*[@]{1}[A-Za-z0-9.|-|_]*[.]{1}[a-z]{2,5}$

From above regular expression:
(1) a@gmail.com ==> valid
(2) b@gmail. ==> invalid
(3) c@gmail ==>invalid

The above regular expression is only for single email address.

But now, I want like below:
(1) a@gmail.com ==> valid
(2) a@gmail.com,b@gmail.com ==> valid
(3) c@gmail,d@gmail.com ==> invalid

The email address should be in full format no matter how many email user fill in.
How should I modify it? Can someone help me?


Use this:

^([A-Za-z0-9.|-|_]*[@]{1}[A-Za-z0-9.|-|_]*[.]{1}[a-z]{2,5})(,[A-Za-z0-9.|-|_]*[@]{1}[A-Za-z0-9.|-|_]*[.]{1}[a-z]{2,5})*?$

The first part will select one email address:

([A-Za-z0-9.|-|_]*[@]{1}[A-Za-z0-9.|-|_]*[.]{1}[a-z]{2,5})

Then the second part is similar, however it must start with a comma ( , ), and the whole section is optional ( ? )

(,[A-Za-z0-9.|-|_]*[@]{1}[A-Za-z0-9.|-|_]*[.]{1}[a-z]{2,5})*?

Check out this demo on RegExr


Thanks to @Wiktor Stribiżew for pointing out my previous RegEx allowed a trailing ,

链接地址: http://www.djcxy.com/p/16528.html

上一篇: 正则表达式拒绝特定的电子邮件

下一篇: 正则表达式为多个电子邮件地址