求一个正则表达式

bashen1101 2011-07-21 04:44:43
怎么描述一个字符串只含有一个字母[a-zA-Z]的情况?

例如:
23423a
B48349
324c434
3498_ c-99
...全文
121 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
bashen1101 2011-07-22
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ch_fb 的回复:]

引用 4 楼 bashen1101 的回复:

引用 2 楼 pmars 的回复:

C# code
^[^a-zA-Z]*?[a-zA-Z][^a-zA-Z]*?$

不对唉。。。

去掉问号!
[/Quote]
不好意思,这个是对的,谢谢
waterstar50 2011-07-22
  • 打赏
  • 举报
回复
bool Check(string str)
{
return Regex.Matches(str,"[a-zA-Z]").Count ==1;
}
netaass123 2011-07-22
  • 打赏
  • 举报
回复
正则菜鸟飞过。。。。。
odp_denden 2011-07-22
  • 打赏
  • 举报
回复
我也想知道,怎么实现
诺维斯基 2011-07-22
  • 打赏
  • 举报
回复
不用那么麻烦的:
re=/[a-zA-Z]{2,}/
然后用,text测试一下。
pmars 2011-07-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 pmars 的回复:]
C# code
^[^a-zA-Z]*?[a-zA-Z][^a-zA-Z]*?$
[/Quote]
想到了字母在最前或最后的情况了!
sxldfang 2011-07-21
  • 打赏
  • 举报
回复
字符串中可包含空格、回车吧,一次只能对一个字符串进行检测:


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
自由建客 2011-07-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 bashen1101 的回复:]

引用 2 楼 pmars 的回复:

C# code
^[^a-zA-Z]*?[a-zA-Z][^a-zA-Z]*?$

不对唉。。。
[/Quote]
去掉问号!
bashen1101 2011-07-21
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 pmars 的回复:]

C# code
^[^a-zA-Z]*?[a-zA-Z][^a-zA-Z]*?$
[/Quote]
不对唉。。。
自由建客 2011-07-21
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 pmars 的回复:]

C# code
^[^a-zA-Z]*?[a-zA-Z][^a-zA-Z]*?$
[/Quote]
为何要加问号?
pmars 2011-07-21
  • 打赏
  • 举报
回复
^[^a-zA-Z]*?[a-zA-Z][^a-zA-Z]*?$
q107770540 2011-07-21
  • 打赏
  • 举报
回复

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

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧