具有键/值对的“紧密”重复模式的字符串(对于此示例,键是“名称”,值应该是单个小写字) string text = "name: abc name: def name: ghi name: jkl"; 应该转换为输出 abc, def, ghi, jkl, 而在模式中的任何
string text = "name: abc name: def name: ghi name: jkl";
应该转换为输出
abc, def, ghi, jkl,
而在模式中的任何干扰(“非紧”所以说)
string text = "name: abc x name: def name: ghi name: jkl";
应该导致比赛失败,这有点像
abc, ## Exception occurred: x cannot be matched to the pattern ##
我试过了
string text = "name: abc name: def name: ghi name: jkl";
string pattern = @"name:\s*([a-z])*\s*";
MatchCollection ms = Regex.Matches(text, pattern);
foreach (Match m in ms)
{
Console.Write(m.Groups[1].Value+", ");
}
但它回来了
c, f, i, l,
是什么导致了这种奇怪的行为,我该如何解决?
与大多数其他正则表达式不同,C#(.Net)的引擎实际上通过Group类的Captures属性跟踪重复捕获.
Group.Captures Property
Gets a collection of all the captures matched by the capturing group, in innermost-leftmost-first order (or innermost-rightmost-first order if the regular expression is modified with the 07001 option).
这意味着通过访问Groups [1](如下面的代码所示)然后访问Captures属性,我们有效地获取每个重复捕获的值在我们的字符串上.
码
See code in use here
using System;
using System.Linq;
using System.Text.RegularExpressions;
class Example {
static void Main() {
string[] strings = new string[]{
"name: abc name: def name: ghi name: jkl",
"name: abc x name: def name: ghi name: jkl"
};
Regex regex = new Regex(@"^(?:name: *([a-z]+) *)+$");
foreach(string s in strings) {
if(regex.IsMatch(s)) {
Match match = regex.Match(s);
Console.WriteLine(string.Join(", ", from Capture c in match.Groups[1].Captures select c.Value));
} else {
Console.WriteLine("Invalid input");
}
}
}
}
结果
name: abc name: def name: ghi name: jkl # abc, def, ghi, jkl name: abc x name: def name: ghi name: jkl # Invalid input
