C# RegEx Pattern - Email But exclude domain

Posted on 2009-02-18
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.
Question by:tradeline
    LVL 39

    Expert Comment

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

    input = input.Replace("","")
    input = input.Replace("","")


    Author Comment

    It's a part of a larger framework, and so it has to occur inside the regex pattern itself.
    LVL 39

    Accepted Solution

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now