• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1968
  • 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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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