简体   繁体   English

正则表达式用于匹配多个电子邮件

[英]Regex for matching multiple emails

Right now I have the regex \\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)* to match an email address, for this situation that is good. 现在我有正则表达式\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*以匹配电子邮件地址,对于这种情况,这是很好的。

I have tried various things for csv cell matching but I can't seem to get them to work for this. 我已经尝试了各种用于csv单元格匹配的方法,但是似乎无法让它们为此工作。

What I need is something that matches the regex above, but have a delimiter between, there are three different delimiters '|' ',' ';' 我需要的是与上面的正则表达式匹配的东西,但是之间有一个定界符,有三个不同的定界符'|' ',' ';' '|' ',' ';' , for example I want to match ,例如我要匹配

example@example.com

example@example.com|example@example.com|example@example.com

example@example.com,example@example.com,example@example.com

example@example.com;example@example.com;example@example.com

I also don't care about whitespace, so it needs to account for them adding extra spaces in, while still accounting for above, for example: 我也不在乎空格,因此它需要考虑到它们在其中添加了额外的空格,同时仍然考虑了上述空格,例如:

example@example.com | example@example.com|example@example.com

It also needs to be able to use a combination of delimiters 它还需要能够使用定界符的组合

example@example.com,example@example.com;example@example.com|example@example.com

I am doing this in c# 我正在用c#

Note, I have looked at this How to match a comma separated list of emails with regex? 注意,我已经看过这个如何用正则表达式匹配用逗号分隔的电子邮件列表?

But I am not sure how to modify ^([\\w+-.%]+@[\\w-.]+\\.[A-Za-z]{2,4},?)+$ to meet my needs 但是我不确定如何修改^([\\w+-.%]+@[\\w-.]+\\.[A-Za-z]{2,4},?)+$以满足我的需求

Currently I have 目前我有

(\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*[\s?[,|\||;|]\s?]?)+$

I'd use a combination of String.Split() and the MailAddress constructor. 我会结合使用String.Split()MailAddress构造函数。 Something like this: 像这样:

public bool ValidAddressText(string input)
{
    string[] addresses = input.Split(new Char[] { ',', ';', '|' });
    foreach (string address in addresses)
    {
        address = address.Trim();
        try
        {
            if (address == "")
            {
                return false;
            }

            new MailAddress(address);
        }
        catch(FormatException)
        {
            return false;
        }
    }

    return true;
}

This will catch all the edge cases that the regular expression won't and even get better over time as the .NET Framework is updated. 随着.NET Framework的更新,这将捕获正则表达式无法使用的所有边缘情况,甚至随着时间的推移会变得更好。 It also simplifies the handling of the delimiters and whitespace. 它还简化了定界符和空格的处理。

Try Something like this (;|\\||,) 尝试这样的事情(;|\\||,)

I recommend the Tool RegExBuddy, you can try your Pattern so Easy with this and it's always a good help for me, to get the right pattern! 我建议使用RegExBuddy工具,您可以尝试使用如此简单的Pattern,这对我来说总是一个很好的帮助,以获得正确的模式! There is a trial Version! 有一个试用版!

I came up with this: 我想出了这个:

(\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*(\s?[,|\||;|]\s?)?)+$

I believe it resolves my problem, any suggestions? 我相信它可以解决我的问题,有什么建议吗?

I'd use lookbehind and lookahead for this: 我会为此使用lookbehind和lookahead:

(?<=\s|\||;|,|^)[\w.+-]+@(?:[\w-]+\.){1,2}(?:[a-z]{2,6})(?:\.[a-z]{2,6})?(?=\s|\||;|,|$)

You can test this regex here . 您可以在此处测试此正则表达式。

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

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