简体   繁体   English

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

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

Trying to validate a comma-separated email list in the textbox with asp:RegularExpressionValidator , see below:尝试使用asp:RegularExpressionValidator验证文本框中以逗号分隔的 email 列表,请参见下文:

<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.我在这篇文章中尝试了一个建议,但无法让它发挥作用。

ps I don't want a discussion on proper email validation ps 我不想讨论正确的 email 验证

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.在括号后添加+表示前面的组可以出现 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.添加^$意味着字符串开头和匹配开头(或匹配结尾和字符串结尾)之间的任何内容都会导致验证失败。

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.以下正则表达式非常适用于逗号分隔的电子邮件 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.第一组将匹配单个 emailId 的字符串。 Second group is optionally required by '*' token ie 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. '*' 标记可选地需要第二组,即此类组的 0 个或多个数量,但 ',' 必须位于此类 emailId 的开头,这使得逗号分隔的 emailId 与上述正则表达式匹配。

A simple modification of @Donut's answer allows adjacent commas, all TLDs of two characters or more, and arbitrary whitespace between email addresses and commas. @Donut 的答案的简单修改允许相邻的逗号、两个或更多字符的所有 TLD 以及电子邮件地址和逗号之间的任意空格。

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

You will need to split and remove whitespace and empty strings on your side, but this should be an overall better user experience.您将需要拆分和删除您身边的空白和空字符串,但这应该是一个整体更好的用户体验。

Examples of matched lists:匹配列表示例:

  • person@example.co,chris@o.com,simon@example.capetown person@example.co,chris@o.com,simon@example.capetown
  • person@example.co ,, chris@o.com, simon@example.capetown person@example.co ,, chris@o.com, simon@example.capetown
^([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.它对电子邮件有点严格,并且输入的电子邮件地址不超过一个,或者根本不存在逗号。

The following RegEx will work even with some of the weirdest emails out there, and it supports a comma between emails.以下 RegEx 甚至可以处理一些最奇怪的电子邮件,并且它支持电子邮件之间的逗号。

((?:[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-zA-Z0-9-]*[a-zA-Z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\]),?)+

A few Examples:几个例子:

Valid: planetearth@solar.com
Valid: planet.earth@solar.com
Valid: planet.earth@solar.com,blue.planet@solar.com
Valid: planet-earth@solar-system.com,/#!$%&'*+-/=?^_`{}|~@solar.org,"!#$%&'-/=^_`{}|~.a"@solar.org
Invalid: planet earth@solar.com

Hope This helps.希望这可以帮助。

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

The point about requiring a comma between groups, but not necessarily at the end is handled here - I'm mostly adding this as it includes a nice subgroup with the (?1) so you only define the actual email address regex once, and then can muck about with delimiters.关于在组之间需要逗号但不一定在末尾处理的要点 - 我主要添加它,因为它包含一个带有(?1)的漂亮子组,因此您只需定义一次实际的电子邮件地址正则表达式,然后可以用分隔符捣乱。

Email address ref here: https://www.regular-expressions.info/email.html电子邮件地址参考这里: https ://www.regular-expressions.info/email.html

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.它允许相邻的逗号。

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

Use the following regex, it will resolve your problem.使用以下正则表达式,它将解决您的问题。 The following regex will entertain post and pre spaces with comma too以下正则表达式也将用逗号招待 post 和 pre 空格

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

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:我建议的正则表达式是这样的:

^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[AZ]{2,}(,[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[AZ]{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".我想出的解决方案不是搜索“一个电子邮件地址后跟一个可选的逗号”一次或多次,这是接受的答案所做的,这个正则表达式搜索“一个电子邮件地址后跟一个可选的逗号前缀的电子邮件地址任意次数”。

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.-]+\.[AZ]{2,} with whichever regex you want to use.您只需将每个[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[AZ]{2,}替换为您要使用的任何正则表达式。

对我有用的解决方案如下

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

The easiest solution would be as following.最简单的解决方案如下。 This will match the string with comma-separated list.这将匹配带有逗号分隔列表的字符串。 Use the following regex in your code.在您的代码中使用以下正则表达式。

Regex: '[^,]+,?'正则表达式:'[^,]+,?'

^([\w+-.%]+@[\w-.]+\.[A-Za-z]+)(, ?[\w+-.%]+@[\w-.]+\.[A-Za-z]+)*$

每个逗号后有 0 或 1 个空格,也适用于长域扩展

This works for me in JS and TS这在 JS 和 TS 中对我有用

^([a-z0-9!#$%&'*+/=?^_`{|}~.-]+@[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*)(([, ]+[a-z0-9!#$%&'*+/=?^_`{|}~.-]+@[a-z0-9]([a-z0-9-]*[a-z0-9])\.([a-z0-9]([a-z0-9-]*[a-z0-9]))*)?)*$

You can check it out here https://regex101.com/r/h0l9ks/1你可以在这里查看https://regex101.com/r/h0l9ks/1

The regex i have for this issue all well except that we need to add comma after every email address.我对这个问题的正则表达式很好,除了我们需要在每个 email 地址后添加逗号。

^((\s*?)[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z,]{2,4}(\s*?),)*

The explanation for this will be like this:对此的解释是这样的:

  1. (\s*?) will allow spaces at the start. (\s*?)将在开头允许空格。
  2. [a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z,]{2,4} is common email pattern. [a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z,]{2,4}是常见的 email 模式。
  3. (\s*?) will allow space at the end too. (\s*?)也会在末尾留出空间。
  4. , will restrict comma. ,将限制逗号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM