I got a good email vaidation regex from:
Email regular expression
public static void Main(string[] args)
{
string value = @"cvcvcvcvvcvvcvcvcvcvcvvcvcvcvcvcvvccvcvcvc";
var regex = 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})$",
RegexOptions.Compiled);
var x = regex.Match(value); // Hangs here !?!
return;
}
It works in most cases, but the code above hangs, burning 100% CPU… I’ve tested in a W8 metro App. and on a standard .Net 4.5 app.
Can anyone tell me why this happens, and if there is a good email validation REGEX that doesn’t hang, or if there is a way to fix this one?
Many thanks,
Jon
The explanation why it hangs: Catastrophic backtracking.
Let’s simplify the crucial part of the regex:
You have
\w*that can match the same characters as the following part[0-9a-zA-Z], so the two combined translate, in essence, to\w+(\w+)*This means that, given
s = "cvcvcvcvvcvvcvcvcvcvcvvcvcvcvcvcvvccvcvcvc", this part of the regex needs to check all possible permutations ofs(which number at2**(len(s)-1)) before deciding on a non-match when the following@is not found.Since you cannot validate an e-mail address with any regex (there are far too many corner cases in the spec), it’s usually best to
^.*@.*$)Just for completeness, you can avoid the backtracking issues with the help of atomic groups: