We help IT Professionals succeed at work.

C# RegEx Pattern - Email But exclude domain

tradeline
tradeline asked
on
Medium Priority
2,255 Views
Last Modified: 2012-06-27
I'm using the following pattern to match emails:

var exp = @"\b[\w\.=-]+@[\w.-]+\.[\w]{2,3}\b";

and this C# to match:

var matches = new Regex(exp , OPTIONS).Matches(input.ToString());

That works.

What I'd like to do now is exclude certain domains (hotmail for example).  So I modified the pattern to be as follows:

var exp = @"\b[\w\.=-]+@(?!.*hotmail).[\w.-]+\.[\w]{2,3}\b";

That works in some of the RegEx tools I've tried, but when I execute the following code in C#:

var matches = new Regex(exp , OPTIONS).Matches(input.ToString());

I get no matches.

Any insight is appreciated.
Comment
Watch Question

Kyle AbrahamsSenior .Net Developer
CERTIFIED EXPERT

Commented:
what if you did a replace for your e-mails?  I'm assuming you want validation to fail?

input = input.Replace("hotmail.com","")
input = input.Replace("yahoo.com","")

etc.

Author

Commented:
It's a part of a larger framework, and so it has to occur inside the regex pattern itself.
Senior .Net Developer
CERTIFIED EXPERT
Commented:
var exp = @"\b[\w\.=-]+@(?!.*hotmail).[\w.-]+\.[\w]{2,3}\b";

Looks like the ! isn't supported in .net?

\w*@(hotmail)+\w* will return anything with hotmail in the name.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.