正则表达式帮助不包含

本文关键字:包含 帮助 正则表达式 | 更新日期: 2023-09-27 18:13:01

我已经忙了一段时间了。我需要一个匹配以下条件的正则表达式:

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

正则表达式帮助不包含

这个正则表达式应该可以工作(与不区分大小写标志一起使用):

 "^(?:(?!dev).)+@mail'.mydomain'.com$"
http://rubular.com/r/hnuvlQorQl

这里,如果一行中没有匹配"dev"

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

try this:

    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));

如果您只是想确定'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