如何用正则表达式得到"[@编号]"里的"编号",一段文字里有多个"[@编号]"

redcomet2004 2008-05-20 04:22:54
如何用正则表达式得到"[@编号]"里的"编号",一段文字里有多个"[@编号]",如[@aaa]asdfagaeg[@bbb]hrhsrth[@ccc],要求返回string[]{"aaa","bbb","ccc"};
...全文
154 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
root_ 2008-05-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 bhtfg538 的回复:]
大哥能解释下 正则
(?<content>[^\]]*)这个地方吗
[/Quote]

(?<name>exp) 是命名捕获组,就是将正则表达式exp匹配到的内容保存到名为name的捕获组内,以供对这一部分内容的后续处理

[^\]]* 这个就是0个或任意多个不是“]”的字符
guilin_gavin 2008-05-20
  • 打赏
  • 举报
回复
楼上正解~学习了~
bhtfg538 2008-05-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 root_ 的回复:]
C# codeList<string> list = new List<string>();
string test = "[@aaa]asdfagaeg[@bbb]hrhsrth[@ccc]";
MatchCollection mc = Regex.Matches(test, @"\[@(?<content>[^\]]*)\]");
foreach (Match m in mc)
list.Add(m.Groups["content"].Value);

//验证结果
foreach (string s in list)
MessageBox.Show(s);
[/Quote]
大哥能解释下 正则
Regex.Matches(test, @"\[@(?<content>[^\]]*)\]");吗?
(?<content>[^\]]*)这个地方吗
我也在学正则
js:var a=/^\\@[(\.*)\]$/g;
var b="[@aaa]asdfagaeg[@bbb]hrhsrth[@ccc]";
var c=b.match(a)
foreach(var i in c)
{
alert(c[i]);
}
root_ 2008-05-20
  • 打赏
  • 举报
回复
List<string> list = new List<string>();
string test = "[@aaa]asdfagaeg[@bbb]hrhsrth[@ccc]";
MatchCollection mc = Regex.Matches(test, @"\[@(?<content>[^\]]*)\]");
foreach (Match m in mc)
list.Add(m.Groups["content"].Value);

//验证结果
foreach (string s in list)
MessageBox.Show(s);

110,538

社区成员

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

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

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