0

Right now I have the regex \w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* to match an email address, for this situation that is good.

I have tried various things for csv cell matching but I can't seem to get them to work for this.

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#

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

Currently I have

(\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*[\s?[,|\||;|]\s?]?)+$
Community
  • 1
  • 1
FabianCook
  • 20,269
  • 16
  • 67
  • 115
  • @hwnd, I've seen e-mail addresses in the form of `john.malcovic+some-subadres@sub.domain.museum.uk` ;) Many internet forms don't accept that. Yes, .museum is a valid domain-extension. I don't know about the `'` though, maybe... – asontu Sep 26 '13 at 22:47
  • Yes `'` made me curious – hwnd Sep 26 '13 at 22:48
  • Actual email addresses can be extremely complicated. More so than most people realise. For example did you know that email addresses can contain comments? http://en.wikipedia.org/wiki/Email_address suggests that `john.smith(comment)@example.com` is a valid email address for example. In general I don't think validating email addresses with Regex is a good idea, mainly because chances are there will always be a valid email address that isn't recognised. Of course you may not always need foolproof... – Chris Sep 26 '13 at 22:59
  • For my situation however I don't really need extreme email validation, if they put it something wrong then it will tell them later on when I put it in a `MailAddress` object – FabianCook Sep 26 '13 at 23:51

4 Answers4

1

I'd use a combination of String.Split() and the MailAddress constructor. 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. It also simplifies the handling of the delimiters and whitespace.

Lee
  • 18,529
  • 6
  • 58
  • 60
0

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! There is a trial Version!

Teifun2
  • 338
  • 2
  • 15
0

I came up with this:

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

I believe it resolves my problem, any suggestions?

FabianCook
  • 20,269
  • 16
  • 67
  • 115
0

I'd use lookbehind and lookahead for this:

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

You can test this regex here.

asontu
  • 4,548
  • 1
  • 21
  • 29