如何匹配一个逗号分隔的电子邮件列表和正则表达式?

时间:2022-10-23 16:58:17

Trying to validate a comma-separated email list in the textbox with asp:RegularExpressionValidator, see below:

尝试在文本框中使用asp:RegularExpressionValidator验证逗号分隔的电子邮件列表:

<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
                    runat="server" ErrorMessage="Wrong email format (separate multiple email by comma [,])" ControlToValidate="txtEscalationEmail"
                    Display="Dynamic" ValidationExpression="([\w+-.%]+@[\w-.]+\.[A-Za-z]{2,4},?)" ValidationGroup="vgEscalation"></asp:RegularExpressionValidator>

It works just fine when I test it at http://regexhero.net/tester/, but it doesn't work on my page.

当我在http://regexhero.net/tester/上测试时,它的效果很好,但是它在我的页面上不起作用。

Here's my sample input:

这是我的示例输入:

test@test.com,test1@test.com

I've tried a suggestion in this post, but couldn't get it to work.

我在这篇文章里试了一个建议,但没能成功。

p.s. I don't want a discussion on proper email validation

我不想讨论正确的电子邮件验证。

7 个解决方案

#1


14  

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.

在括号后面加上+,意味着前面的组可以出现1次或更多次。

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.

添加^和$意味着任何字符串的开始与匹配的开始(或结束的比赛和结束的字符串)导致验证失败。

#2


17  

This Regex will allow emails with spaces after the commas.

这个Regex将允许在逗号之后使用空格的电子邮件。

^[\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.

与此同时,一位同事想出了这个更准确的正则表达式。上面的答案似乎是通过电子邮件地址列表,其中第一个元素不是电子邮件地址。这里是更新,也允许在逗号之后空格。

#3


1  

^([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+,*[\W]*)+$

This will also work. It's a little bit stricter on emails, and doesn't that there be more than one email address entered or that a comma be present at all.

这也将工作。它对电子邮件的要求更严格,而且不存在一个以上的电子邮件地址或者一个逗号。

#4


1  

The first answer which is selected as best matches the string like abc@xyz.comxyz@abc.com which is invalid.

选择的第一个答案是最匹配的字符串,如abc@xyz.comxyz@abc.com,它是无效的。

The following regex will work for comma separated email ids awesomely.

下面的regex将用于逗号分隔的电子邮件id。

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

It will match single emailId, comma separated emailId but not if comma is missed.

它将匹配单一的emailId,逗号分隔的emailId,但如果没有逗号,则不会。

First group will match string of single emailId. Second group is optionally required by '*' token i.e. either 0 or more number of such group but ',' is required to be at the beginning of such emailId which makes comma separated emailId to match to the above regex.

第一组将匹配单个emailId的字符串。第二组是可选的“*”标记,即0或更多这样的组,但是“,”需要在这样的emailId开始时,以逗号分隔的emailId与上面的regex匹配。

#5


0  

The regex below is less restrictive and more appropriate for validating a manually-entered list of comma-separated email addresses. It allows for adjacent commas.

下面的regex限制更少,更适合验证手动输入的逗号分隔的电子邮件地址列表。它允许相邻的逗号。

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

#6


0  

Use the following regex, it will resolve your problem. The following regex will entertain post and pre spaces with comma too

使用下面的regex,它将解决您的问题。下面的regex将用逗号来招待post和pre空格。

/^((([a-zA-Z0-9_-.]+)@([a-zA-Z0-9_-.]+).([a-zA-Z\s?]{2,5}){1,25})(\s?,\s*?))$/

#7


0  

I'm a bit late to the party, I know, but I figured I'd add my two cents, since the accepted answer has the problem of matching email addresses next to each other without a comma.

我知道,我有点晚了,但我想我会加上我的两美分,因为这个被接受的答案有一个问题,就是在没有逗号的情况下,把电子邮件地址互相匹配。

My proposed regex is this:

我提议的regex是这样的:

^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}(,[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,})*$

^[a - z0 - 9. - _ % + -)+ @(- z0 - 9.)+ \[a - z]{ 2,}(,(- z0 - 9. - _ % + -)+ @[- z0 - 9.)+ \[a - z]{ 2,})*美元

It's similar to the accepted answer, but solves the problem I was talking about. The solution I came up with was instead of searching for "an email address followed by an optional comma" one or more times, which is what the accepted answer does, this regex searches for "an email address followed by an optional comma prefixed email address any number of times".

这类似于被接受的答案,但解决了我正在谈论的问题。我提出的解决方案不是搜索“一个电子邮件地址,后面是一个可选逗号”,而是一个或多个时间,这是被接受的答案所做的,这个regex搜索“一个电子邮件地址,然后是一个可选的逗号前缀的电子邮件地址,任何次数”。

That solves the problem by grouping the comma with the email address after it, and making the entire group optional, instead of just the comma.

这就解决了这个问题,将逗号分组到后面的电子邮件地址,并使整个组成为可选的,而不是仅仅使用逗号。

Notes: This regex is meant to be used with the insensitive flag enabled.
You can use whichever regex to match an email address you please, I just used the one that I was already using. You would just replace each [A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,} with whichever regex you want to use.

注意:这个正则表达式是用来启用不敏感的标志的。您可以使用任何regex来匹配您的电子邮件地址,我只是使用了我已经使用过的。你只会取代每个[a - z0 - 9. - _ % + -)+ @(- z0 - 9.)+ \。[A-Z]{2,}以任何你想使用的正则表达式。

#1


14  

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.

在括号后面加上+,意味着前面的组可以出现1次或更多次。

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.

添加^和$意味着任何字符串的开始与匹配的开始(或结束的比赛和结束的字符串)导致验证失败。

#2


17  

This Regex will allow emails with spaces after the commas.

这个Regex将允许在逗号之后使用空格的电子邮件。

^[\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.

与此同时,一位同事想出了这个更准确的正则表达式。上面的答案似乎是通过电子邮件地址列表,其中第一个元素不是电子邮件地址。这里是更新,也允许在逗号之后空格。

#3


1  

^([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+,*[\W]*)+$

This will also work. It's a little bit stricter on emails, and doesn't that there be more than one email address entered or that a comma be present at all.

这也将工作。它对电子邮件的要求更严格,而且不存在一个以上的电子邮件地址或者一个逗号。

#4


1  

The first answer which is selected as best matches the string like abc@xyz.comxyz@abc.com which is invalid.

选择的第一个答案是最匹配的字符串,如abc@xyz.comxyz@abc.com,它是无效的。

The following regex will work for comma separated email ids awesomely.

下面的regex将用于逗号分隔的电子邮件id。

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

It will match single emailId, comma separated emailId but not if comma is missed.

它将匹配单一的emailId,逗号分隔的emailId,但如果没有逗号,则不会。

First group will match string of single emailId. Second group is optionally required by '*' token i.e. either 0 or more number of such group but ',' is required to be at the beginning of such emailId which makes comma separated emailId to match to the above regex.

第一组将匹配单个emailId的字符串。第二组是可选的“*”标记,即0或更多这样的组,但是“,”需要在这样的emailId开始时,以逗号分隔的emailId与上面的regex匹配。

#5


0  

The regex below is less restrictive and more appropriate for validating a manually-entered list of comma-separated email addresses. It allows for adjacent commas.

下面的regex限制更少,更适合验证手动输入的逗号分隔的电子邮件地址列表。它允许相邻的逗号。

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

#6


0  

Use the following regex, it will resolve your problem. The following regex will entertain post and pre spaces with comma too

使用下面的regex,它将解决您的问题。下面的regex将用逗号来招待post和pre空格。

/^((([a-zA-Z0-9_-.]+)@([a-zA-Z0-9_-.]+).([a-zA-Z\s?]{2,5}){1,25})(\s?,\s*?))$/

#7


0  

I'm a bit late to the party, I know, but I figured I'd add my two cents, since the accepted answer has the problem of matching email addresses next to each other without a comma.

我知道,我有点晚了,但我想我会加上我的两美分,因为这个被接受的答案有一个问题,就是在没有逗号的情况下,把电子邮件地址互相匹配。

My proposed regex is this:

我提议的regex是这样的:

^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}(,[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,})*$

^[a - z0 - 9. - _ % + -)+ @(- z0 - 9.)+ \[a - z]{ 2,}(,(- z0 - 9. - _ % + -)+ @[- z0 - 9.)+ \[a - z]{ 2,})*美元

It's similar to the accepted answer, but solves the problem I was talking about. The solution I came up with was instead of searching for "an email address followed by an optional comma" one or more times, which is what the accepted answer does, this regex searches for "an email address followed by an optional comma prefixed email address any number of times".

这类似于被接受的答案,但解决了我正在谈论的问题。我提出的解决方案不是搜索“一个电子邮件地址,后面是一个可选逗号”,而是一个或多个时间,这是被接受的答案所做的,这个regex搜索“一个电子邮件地址,然后是一个可选的逗号前缀的电子邮件地址,任何次数”。

That solves the problem by grouping the comma with the email address after it, and making the entire group optional, instead of just the comma.

这就解决了这个问题,将逗号分组到后面的电子邮件地址,并使整个组成为可选的,而不是仅仅使用逗号。

Notes: This regex is meant to be used with the insensitive flag enabled.
You can use whichever regex to match an email address you please, I just used the one that I was already using. You would just replace each [A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,} with whichever regex you want to use.

注意:这个正则表达式是用来启用不敏感的标志的。您可以使用任何regex来匹配您的电子邮件地址,我只是使用了我已经使用过的。你只会取代每个[a - z0 - 9. - _ % + -)+ @(- z0 - 9.)+ \。[A-Z]{2,}以任何你想使用的正则表达式。