1

How can I rewrite this regular expression in order to match all email addresses, but not those
which contains "hotmail,gmail and yahoo". So far I wrote this:

^([a-zA-Z0-9_\-\.]+)@(?<!hotmail|gmail|yahoo)((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$
coder
  • 658
  • 3
  • 14
  • 31
  • More localized version: _"C#-Regular expression for email, but exclude “John@gmail.com”"_ – gdoron Mar 25 '13 at 09:42
  • 2
    **Don't use regular expressions to validate e-mail addresses!** http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address – Carsten Mar 25 '13 at 09:43
  • Ok. I will do it in c#.Thank you for recommendation – coder Mar 25 '13 at 09:50

1 Answers1

5

Change the negative lookbehind to a negative lookahead by removing the <, and reposition it as follows

^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(?!hotmail|gmail|yahoo)(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$

The above assumes that "hotmail,gmail and yahoo" would directly follow the @.

Shorter equivalent:

@"^([\w.-]+)@(\[(\d{1,3}\.){3}|(?!hotmail|gmail|yahoo)(([a-zA-Z\d-]+\.)+))([a-zA-Z]{2,4}|\d{1,3})(\]?)$"
MikeM
  • 13,156
  • 2
  • 34
  • 47
  • Hello, I tried your solution but this email does not match the RegEx : aa@gmaill.com. It should match because it's "gmaill" and not "gmail" – Aymen Jul 17 '20 at 09:05