Ramzy Abourafeh
Ramzy Abourafeh

Reputation: 1195

Regular expression for multiple email destinations delimited by semicolon

I succeed to do regular expression for single email like this:

private readonly Regex _regex = new Regex(@"^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
                                          + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");

Now, I need to make regular expression for more than one email, and delimited by semicolon

[email protected];[email protected];[email protected]

I found this regular expression:

\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*([,;]\s*\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)*

but the problem that this regular expression also "receive" this string:

[email protected];asds@gmail

How can I do that?

Thanks

Upvotes: 1

Views: 588

Answers (1)

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

Let's not build a regular expression for more than one because this one is complex enough, just validate them individually:

foreach (var email in emailList.Split(new char[] { ';' },
    StringSplitOptions.RemoveEmptyEntries))
{
    // validate email
}

further, regular expressions aren't all that well suited for something like this because the user could have put a space, more than one space, before or after the semicolon - it just gets messy - and emails are messy enough already.

Upvotes: 2

Related Questions