正则表达式帮助不包含

时间:2011-11-01 04:16:41

标签: c# javascript regex

我已经旋转了很长一段时间。我需要一个符合以下条件的正则表达式:

anytextherethatdoesNOTcontainDEVString@mail.mydomain.com

例如:

first.last@mail.mydomain.com //match
firstLast@mail.mydomain.com //match
name@mail.mydomain.com //match
dev.test@mail.mydomain.com //NON-match contains "dev" string case non-sensitive
dev@mail.mydomain.com //NON-match contains "dev" string case non-sensitive

4 个答案:

答案 0 :(得分:1)

这个正则表达式应该可行(使用不区分大小写的标志):

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

http://rubular.com/r/hnuvlQorQl

答案 1 :(得分:0)

这里,如果没有一行匹配“dev”

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

答案 2 :(得分:0)

试试这个:

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

答案 3 :(得分:0)

如果您只是想确定'dev'是否出现在这些字符串中的任何位置:

var addresses = new[] {
    "first.last@mail.mydomain.com",
    "DeV.test@mail.mydomain.com",
    "first.last@dev.mydomain.com"
};
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);
}

<强>输出

first.last@mail.mydomain.com contains dev: false
DeV.test@mail.mydomain.com contains dev: true
first.last@dev.mydomain.com contains dev: true

或者,如果您只想检查“@”左侧地址的一部分,则使用带有Regex.IsMatch()的简单正则表达式将起作用:

var addresses = new[] {
    "first.last@mail.mydomain.com",
    "DeV.test@mail.mydomain.com",
    "first.last@dev.mydomain.com"
};
var pattern = @"dev.*@";
foreach(var address in addresses)
{
    var hasDevOnLeft = Regex.IsMatch(address, pattern, RegexOptions.IgnoreCase);
    Console.WriteLine("{0} matches: {1}", address, hasDevOnLeft);
}

<强>输出

first.last@mail.mydomain.com matches: false
DeV.test@mail.mydomain.com matches: true
first.last@dev.mydomain.com matches: false