正则表达式检查某些字符

时间:2014-03-18 09:10:02

标签: c# regex asp.net-mvc

我想检查字符串是否包含两个引号,例如“'和括号(...)出现在字符串中的任何位置。

例如

string s = "The flora and fauna of Britain \"has been transported to almost every corner of the globe since colonial times\" (Plants and Animals of Britain, 1942: 8).";

string x = "Morris et al (2000: 47) state 'that the debate of these particular issues should be left to representative committees.'";

我想出了这个:

Regex.IsMatch(x, @"(?<=""')[^\""]*(?=""')")

但是,我想我会尝试使用@RB

提到的string.Contains

@musefan,字符串s和y都是正面结果,引号必须打开和关闭。

1 个答案:

答案 0 :(得分:0)

您可以使用LINQ方法使用Count执行此操作:

string s = "The flora and fauna of Britain \"has been transported to almost every corner of the globe since colonial times\" (Plants and Animals of Britain, 1942: 8).";

bool isMatch = s.Count(c => c == '"') % 2 == 0  ||
               s.Count(c => c == '\'') % 2 == 0 ||
               (s.Count(c => c == '(') == s.Count(c => c == ')'));

如果您想确保该字符串包含所有字符" ' ( ),只需将||更改为&&

相关问题