0

I am using this regex in C# for email format validation based on information from http://www.regular-expressions.info/email.html:

Regex.IsMatch("test@gmail.com", 
              @"^[A-Z0-9'._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$",RegexOptions.IgnoreCase);

I would also like to validate that the total length of the email is between 5 - 254 characters. How should this regex be modified to satisfy the length condition? I don't want to check the length of the string in C# explicitly.

Simon MᶜKenzie
  • 8,344
  • 13
  • 50
  • 77
softveda
  • 10,858
  • 6
  • 42
  • 50
  • If you look over the voluminous number of questions on SO re email regex validation, you'll see that it's a bad idea. The _only_ way to be certain is to send a link to the address to validate that (1) it's a valid address; and (2) it's a _real_ address. – paxdiablo Feb 27 '13 at 06:15
  • http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address – Ravi Gadag Feb 27 '13 at 06:17
  • 1
    @paxdiablo Everything has a context. In my context I am trying to prevent junk data being captured with some false negatives being allowed. I am not trying to be 100% successful nor I am trying to validate that the email actually exists. The above regex is good enough for my purpose. What I am also trying to do is to maintain the max possible length of an email which is 254 characters. – softveda Feb 27 '13 at 08:56
  • @Ravi that regex is not too dissimilar to the one I'm using. But it doesn't validate the length. – softveda Feb 27 '13 at 08:57
  • Well then, can I ask _why_ you don't want to check the string length? Surely an `if (Regex.IsMatch(s, "blah blah") && (len(s) >=5) && (len(s) <= 254))` is simpler than an even _more_ complex regex (assuming it's even possible, given the disparate fields you're processing in said regex). – paxdiablo Feb 27 '13 at 10:53
  • @paxdiablo The length checking is possible using a forward look ahead as answered by Simon. The reason to use a regex is because this regex will be centrally stored in DB and can be retrieved and used by multiple layers in clients (.Net and non-.Net) and services and we have decided that all validation is just using the canonical regex. – softveda Feb 28 '13 at 10:24

2 Answers2

1

Although it's probably cleaner to just do the length checks separately, you can incorporate your length constraint by adding a noncapturing lookahead to the start of your expression:

^(?=.{5,254})[A-Z0-9'._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$
Simon MᶜKenzie
  • 8,344
  • 13
  • 50
  • 77
0

I suggest you not put such restriction. if you really want the regex. As paxdiablo said many number of questions exist in SO. Email validation in regex

^.{1,254}$

http://rick.measham.id.au/paste/explain.pl?regex=%5E.%7B1%2C150%7D%24

Community
  • 1
  • 1
Ravi Gadag
  • 15,735
  • 5
  • 57
  • 83