TechnoJay
TechnoJay

Reputation: 31

Regular Expression Help NOT Contains

I've been spinning my wheels for quite sometime. I need a regular expression that matches the following conditions:

[email protected]

For example:

[email protected] //match
[email protected] //match
[email protected] //match
[email protected] //NON-match contains "dev" string case non-sensitive
[email protected] //NON-match contains "dev" string case non-sensitive

Upvotes: 1

Views: 240

Answers (4)

Brock Spratlen
Brock Spratlen

Reputation: 1

If you simply want to determine if 'dev' appears anywhere in those strings:

var addresses = new[] {
    "[email protected]",
    "[email protected]",
    "[email protected]"
};
foreach(var address in addresses)
{
    // unfortunately C#'s String.Contains does not have an ignore case option
    // hack to use indexOf instead (which does provide such an option)
    var hasDev = (address.IndexOf("dev", StringComparison.OrdinalIgnoreCase) != -1);
    Console.WriteLine("{0} contains dev: {1}", address, hasDev);
}

Output

[email protected] contains dev: false
[email protected] contains dev: true
[email protected] contains dev: true

Alternatively, if you only want to check the part of the address to the left of the '@', using a simple regular expression with Regex.IsMatch() would work:

var addresses = new[] {
    "[email protected]",
    "[email protected]",
    "[email protected]"
};
var pattern = @"dev.*@";
foreach(var address in addresses)
{
    var hasDevOnLeft = Regex.IsMatch(address, pattern, RegexOptions.IgnoreCase);
    Console.WriteLine("{0} matches: {1}", address, hasDevOnLeft);
}

Output

[email protected] matches: false
[email protected] matches: true
[email protected] matches: false

Upvotes: 0

ojlovecd
ojlovecd

Reputation: 4892

try this:

    string[] array = { "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]" };
    Regex reg = new Regex(@"(?is)^(?!.*?dev).+@mail\.mydomain\.com$");
    foreach (string s in array)
        Console.WriteLine(reg.IsMatch(s));

Upvotes: 0

Niall Byrne
Niall Byrne

Reputation: 2460

This regex should work (use it with the case insensitive flag):

 "^(?:(?!dev).)+@mail\.mydomain\.com$"

http://rubular.com/r/hnuvlQorQl

Upvotes: 1

Khan
Khan

Reputation: 516

Here, if none matches "dev" on one line

// for each line input    
Match match = Regex.Match(input, @"dev", RegexOptions.IgnoreCase);     
if (!match.Success) {       
// here you have non matching     
}

Upvotes: 0

Related Questions