• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2039
  • Last Modified:

C# RegEx Pattern - Email But exclude domain

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.
0
tradeline
Asked:
tradeline
  • 2
1 Solution
 
Kyle AbrahamsSenior .Net DeveloperCommented:
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.
0
 
tradelineAuthor Commented:
It's a part of a larger framework, and so it has to occur inside the regex pattern itself.
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now