tags:

views:

228

answers:

5

can someone write a regex in C# for me to verify the following emails ?

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

every email addresses are seperated with ";", and I have wrote the following regex:

^(([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9});)*$

when use this regex to match a string, it might cause a dead loop, why ?

Thanks in advance!

+2  A: 

I think you should split the email addresses and match each one against a regular expression for matching email.

  1. Split the email addresses using ','

  2. Match each email address against a validation expression.

rahul
but do you know why the regex above will cause problem ?
MemoryLeak
I really want to know why this will cause problem, since i just add a ":" at the end of the email regex, and then surround with ()*
MemoryLeak
A: 

Your specific example works:

string s = "[email protected];[email protected];[email protected];[email protected];";
Regex re = new Regex(@"^(([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9});)*$");

Console.WriteLine(re.IsMatch(s));

prints "True"

Hans Kesting
A: 

I could not reproduce the infinite loop on my computer with your example (I am using .NET 3.5). Here the code I used:

Regex rex = new Regex(@"^(([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*"+
    @"@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9});)*$");
String emails = "[email protected];[email protected];[email protected];[email protected];";
Boolean ismatch = rex.IsMatch(emails);
Match match = rex.Match(emails);

ismatch result is true and match contains data.

This other question : How to avoid infinite loops in the .NET RegEx class? might be of interest to you.

jdehaan
A: 

You can try using this

 \s*[;,]\s*(?!(?<=(?:^|[;,])\s*"(?:[^"]|""|\\")*[;,]\s*)(?:[^"]|""|\\")*"\s*(?:[;,]|$))

This regex splits comma or semicolon separated lists of optionally quoted strings. It also handles quoted delimiters and escaped quotes. Whitespace inside quotes is preserved, outside the quotes is removed.

Hope this will meet your requirement

Thanks

~ Aamod

Aamod
A: 

Your regular expression suffers from catastrophic backtracking. I added atomic groups to your regular expression to create this:

^(([0-9a-zA-Z](?>(?>[-\.\w]*[0-9a-zA-Z])*@)(?>(?>[0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9}));)*$
Jeremy Stein