1

We have an application where we are using the following RegEx to validate email.

private string _regularExpression = @"^(("".+?"")|([0-9a-zA-Z](((\.(?!\.))|([-!#\$%&'\*\+/=\?\^`\{\}\|~\w]))*[0-9a-zA-Z])*))@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9}$";
Regex _reg = new Regex(_regularExpression);

However we have a incorrect email in source:

abcd1090ihavenoidea.what.going.wrong.here

for which the program hangs. We are still unable finding out which part we are doing wrong here?

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
SamGhatak
  • 1,487
  • 1
  • 16
  • 27
  • take this one `^([A-Z|a-z|0-9](\.|_){0,1})+[A-Z|a-z|0-9]\@([A-Z|a-z|0-9])+((\.){0,1}[A-Z|a-z|0-9]){2}\.[a-z]{2,3}$` – Nitro.de Mar 17 '16 at 13:02
  • 1
    @PatrickHofman It actually does on my computer – Matias Cicero Mar 17 '16 at 13:03
  • How would it? It doesn't even use the email address mentioned. – Patrick Hofman Mar 17 '16 at 13:05
  • 1
    One update, I just tried out it in [here](https://regex101.com/) , it says *Catastrophic backtracking*...any common way to fix it? – SamGhatak Mar 17 '16 at 13:08
  • @Nitro.de thanks mate, but we got rid of it using timeout, just wanted to know what went wrong here. – SamGhatak Mar 17 '16 at 13:12
  • 1
    Please do not use regex to parse or validate email addresses. Whereas it's possible, the regex is incredibly complicated to get right for all valid addresses. Instead, use something like what's described in http://stackoverflow.com/questions/1365407/c-sharp-code-to-validate-email-address – Jim Mischel Mar 17 '16 at 14:25

2 Answers2

3

The program hangs because with this regex you get a Catastrophic backtracking. It stopped on Regex 101 after 121 628 steps. I try to lower the input string to see how catastrophic it was: it took 88 063 steps to understand that abcd1090ihav.w is not a valid email.

Since you're using , you may read :

Community
  • 1
  • 1
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
1

EDIT Although I don't think regex is the right tool for validating emails, if OP prefers to use it, I found the following long pattern from HERE, which claims the best match. And the site is dedicated for regex solutions:

^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$

REGEX DEMO.

Also take a look at this site: Stop Validating Email Addresses With Regex

Quinn
  • 4,394
  • 2
  • 21
  • 19