0

I use the following expression to validate email addresses (I found it on internet somewhere, so don't blame me if it's totally wrong :-) ):

^((?:(?:(?:[a-zA-Z0-9_][.-+]?)*)[a-zA-Z0-9_][.-+]?)*)\@((?:(?:(?:[a-zA-Z0-9][.-_]?){0,62})[a-zA-Z0-9])+).([a-zA-Z0-9]{2,6})$

It works fine in most cases, but for some reason adding a space somewhere in the email address makes the IsMatch method very slow. It seems that the computation time grows with the position of the space in the email address. The following not valid email addresses illustrate the problem.

test@aweb serversomewhereintheworld.com (slow)

test@awebserversomewhere intheworld.com (very slow)

What's wrong with the regular expression that is used to validate the email addresses?

Bjarke
  • 21
  • 2
  • 5
  • just find spaces, i think... or make your own parser :) or better regex.( separate it into 3 regexs, each one for every part of e-mail). – gaussblurinc May 10 '12 at 10:03
  • As an aside, we have yet to find a regex email validator which correctly accepts the full set of allowable RFC822 addresses, and nothing else. – tripleee May 10 '12 at 10:23

1 Answers1

1

Since spaces aren't valid in email addresses, just have a separate check for spaces before doing the full validation, and if any are found, don't do the full (slow) validation.

Also, for a full discussion about what's wrong with using regex for email addresses, see this question: Using a regular expression to validate an email address

Community
  • 1
  • 1
Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
  • Quoted spaces are actually allowed by RFC822, although I don't think I have seen an address in real life which actually required the quoted-atom syntax. – tripleee May 16 '12 at 07:39
  • @tripleee: Thanks, didn't know that. Though from a practical point of view I'd probably still just ignore any email address with spaces (as well as any containing backspaces etc). – Hans Olsson May 16 '12 at 10:08