请问,这个正则表达式如何写

drse23432 2012-03-17 07:11:55
string sentence = "×××××";

一个单词,有5个字母,如何只找出第3个字母,或者只找出第一个字母,其他的不找。因为,如果一个匹配结果太多的话,再去遍历寻找,效率会低。
...全文
57 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
EnForGrass 2012-03-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 dalmeeme 的回复:]
找出第一个字母
C# code
string s = @"Let us do morning-execises!";
MatchCollection matches = Regex.Matches(s, @"(?<=^|[,;'\-\s]+)[A-Za-z]");
foreach (Match match in matches)
……
[/Quote]
支持一个

string ss = @"Let us do morning-execises!";
MatchCollection matches = Regex.Matches(ss, @"(?<=^|[\W]+)[A-Za-z]");
foreach (Match match in matches)
MessageBox.Show(match.Value);
dalmeeme 2012-03-17
  • 打赏
  • 举报
回复

找出第一个字母
		string s = @"Let us do morning-execises!";
MatchCollection matches = Regex.Matches(s, @"(?<=^|[,;'\-\s]+)[A-Za-z]");
foreach (Match match in matches)
Response.Write(match.Value + "<br/>");

找出第三个字母(更正)
string s = @"Let us do morning-execises!";
MatchCollection matches = Regex.Matches(s, @"(?<=^|[,;'\-\s]+[A-Za-z]{2})[A-Za-z]");
foreach (Match match in matches)
Response.Write(match.Value + "<br/>");
dalmeeme 2012-03-17
  • 打赏
  • 举报
回复
找出第3个字母:
		string s = @"Let us do morning-execises!";
MatchCollection matches = Regex.Matches(s, @"(?<=[,;'\-\s]+[A-Za-z]{2})[A-Za-z]");
foreach (Match match in matches)
Response.Write(match.Value + "<br/>");

111,126

社区成员

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

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

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