2011-11-01 162 views
1

我一直在旋转我的轮子很长一段时间。我需要符合以下条件的正则表达式:正则表达式帮助不包含

[email protected] 

例如:

[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 
+0

'布尔匹配=!regex.matches();' – bdares

+1

对于这样一个简单的事情,为什么不简单地使用简单的字符串搜索?正则表达式是针对“复杂”模式的,但是你想看看是否有3个字符“dev”存在或不存在。正则表达式是巨大的矫枉过正。 –

+0

我需要一个REGEX的原因是我想要更改匹配/搜索字符串的灵活性,而无需重新编译和重新部署应用程序。相反,我更改了配置文件中的REGEX。 – TechnoJay

回答

0

这里,如果没有“开发”匹配在一行

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

如果你只是想确定是否 '开发' 的任何地方出现在这些字符串:

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

输出

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

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

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

输出

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