C# 正则表达式匹配不带';不包含字母和数字?
我想使用正则表达式检查不包含字母或数字的字符串,以便将其删除C# 正则表达式匹配不带';不包含字母和数字?,c#,nlp,C#,Nlp,我想使用正则表达式检查不包含字母或数字的字符串,以便将其删除 Ex: "().;" => false "(9).;" => true ")+&^%" => false "A)%$#" => true 感谢和问候, 陈秀 或 这是一个不使用正则表达式的单行程序: private static void Main() { Console.WriteLine(TestAlphaNumeric("().;"));
Ex: "().;" => false
"(9).;" => true
")+&^%" => false
"A)%$#" => true
感谢和问候,
陈秀
或
这是一个不使用正则表达式的单行程序:
private static void Main()
{
Console.WriteLine(TestAlphaNumeric("().;"));
Console.WriteLine(TestAlphaNumeric("(9).;"));
Console.WriteLine(TestAlphaNumeric(")+&^%"));
Console.WriteLine(TestAlphaNumeric("A)%$#"));
Console.Read();
}
private static bool TestAlphaNumeric(string str)
{
return str.Any(char.IsLetterOrDigit);
}
输出:
假的
真的
假的
真的
那你为什么不用呢?我建议你用直的C
var shouldKeep = System.Text.RegularExpressions.Regex.IsMatch("\\w", myString);
private static void Main()
{
Console.WriteLine(TestAlphaNumeric("().;"));
Console.WriteLine(TestAlphaNumeric("(9).;"));
Console.WriteLine(TestAlphaNumeric(")+&^%"));
Console.WriteLine(TestAlphaNumeric("A)%$#"));
Console.Read();
}
private static bool TestAlphaNumeric(string str)
{
return str.Any(char.IsLetterOrDigit);
}
string pattern = @"(\w|\d)";