0

I am trying to validate a csv file of domain names (eg. @google.com, @xyz.co.uk, @xyz.edu etc.)

I am using the following Regular Expression:

Regex(@"@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,4}$", RegexOptions.Compiled);

Is there scope for improvement on the above?

GoldenUser
  • 365
  • 3
  • 6
  • 21
  • 1
    See http://stackoverflow.com/questions/399932/can-i-improve-this-regex-check-for-valid-domain-names – Ina Apr 17 '12 at 14:43
  • This is a decent first line of defense, but it's too easy to fool *intentionally*. Take a look at the accepted answer of the linked question, it is a much better approach. – Sergey Kalinichenko Apr 17 '12 at 14:46
  • I tried to do [e-mail validation](http://www.regular-expressions.info/email.html) once, and as you can see, there are many different regex's (especially after the `@` symbol) you can use. –  Apr 17 '12 at 14:48

1 Answers1

1

Familiarize yourself with some dedicated sites.

Pick a solution that does NOT require constant maintenance. Your own regex looks pretty good for some applications where you do not expect outright attacks.

See also the answer linked by @lna in a comment.

Jirka Hanika
  • 13,301
  • 3
  • 46
  • 75
  • OK. But with my regular expression above... I am able to return abc@gmail.com as True... which is incorrect... – GoldenUser Apr 17 '12 at 22:27
  • 1
    Because it is not anchored. Add `^` at the beginning and `$` at the end, if you want the whole input string to match and not just any substring. – Jirka Hanika Apr 18 '12 at 06:49