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

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