本文介绍了正则表达式.NET连接的命名组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获得附加命名组。
来源文本:
1/2/3/4/5|id1:value1|id2:value2|id3:value3|1/4/2/7/7|id11:value11|id12:value12|
Group1:
1/2/3/4/5|id1:value1|id2:value2|id3:value3|
Sub groups:
id1:value1|
id2:value2|
id3:value3|
Group2:
1/4/2/7/7|id11:value11|id12:value12|
Sub groups:
id11:value11|
id12:value12|
我怎样才能做到这一点?
How I can do this?
推荐答案
虽然这个任务是很容易的无并发症通过拆分,净正则表达式匹配召开各组的所有捕获的记录(不同于任何其他的味道,我知道的),使用组。捕获集合。
While this task is easy enough without the complication by splitting, .Net regex matches hold a record of all captures of every group (unlike any other flavor that I know of), using the Group.Captures collection.
匹配:
string pattern = @"(?<Header>\d(?:/\d)*\|)(?<Pair>\w+:\w+\|)+";
MatchCollection matches = Regex.Matches(str, pattern);
使用:
foreach (Match match in matches)
{
Console.WriteLine(match.Value); // whole match ("Group1/2" in the question)
Console.WriteLine(match.Groups["Header"].Value);
foreach (Capture pair in match.Groups["Pair"].Captures)
{
Console.WriteLine(pair.Value); // "Sub groups" in the question
}
}
工作的例子: http://ideone.com/5kbIQ
这篇关于正则表达式.NET连接的命名组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!