字符串中可包含空格、回车吧,一次只能对一个字符串进行检测: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.IO; namespace sxLdfang { class Program { static void Main(string[] args) { string html = @"23423G 678 "; string pattern = @"^[^A-Za-z]*[A-Za-z][^A-Za-z]*$"; MatchCollection mc = Regex.Matches(html, pattern); foreach (Match m in mc) { Console.WriteLine(m.Value); } Console.ReadKey(); } } } 运行结果: 23423G 678
void Main()
{
List<string>list=new List<string>
{
"23423a",
"B48349",
"324c434d",
"3498_ c-99"
};
foreach(string str in list)
{
Console.WriteLine("{0}:\t{1}",str,Check(str));
}
/*
23423a: True
B48349: True
324c434d: False
3498_ c-99: True
*/
}
bool Check(string str)
{
return Regex.Matches(str,"[a-zA-Z]").Count ==1;
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
namespace sxLdfang
{
class Program
{
static void Main(string[] args)
{
string html = @"23423G 678
";
string pattern = @"^[^A-Za-z]*[A-Za-z][^A-Za-z]*$";
MatchCollection mc = Regex.Matches(html, pattern);
foreach (Match m in mc)
{
Console.WriteLine(m.Value);
}
Console.ReadKey();
}
}
}
运行结果:
23423G 678
re=/[a-zA-Z]{2,}/
然后用,text测试一下。
{
return Regex.Matches(str,"[a-zA-Z]").Count ==1;
}