什么是非捕获组?如(?:"(?<1>[^]"]*)具体能匹配一些什么字符串?

heavenkiller 2003-11-14 10:26:59
什么是非捕获组?如(?:"(?<1>[^]"]*)具体能匹配一些什么字符串?
...全文
57 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
saucer 2003-11-15
  • 打赏
  • 举报
回复
(...) captures something, which can be backreferenced ethrough \number

(?<name>..) also captures something, which can be backreferenced through \k<name>

see
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconbackreferenceconstructs.asp

but (?:...) is just a grouping construct and doesn't capture anything

(?:"(?<1>[^]"]*))

can match

"anything not " and ]

so if you have

1. abcdef"ghijk]

the expression will match

"ghijk

and with $1 having

ghijk

2. abcdef"ghijk"

the expression will match

"ghijk

and with $1 having

ghijk


string[] slist = {"abcdef\"ghijk]", "abcdef\"ghijk\"","abd"};

Regex re = new Regex(@"(?:""(?<1>[^]""]*))");

foreach (string s in slist)
{
Match m = re.Match(s);
Console.WriteLine("{0} matches? {1}\tthe matched part is {2} and $1 is {3}", s, m.Success, m.Value, m.Groups[1].Value);
}

110,538

社区成员

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

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

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