在 C#、VS 2010 中使用正则表达式。这是代码。
static string capturePattern = @"\|([!-|]{2})([!-|]{2})([!-|]{2})?([!-|]{2})?([!-|]{2})?([!-|]{2})?([!-|]{2})?\|";
Regex rgx = new Regex(capturePattern);
string TS="!3829.87N/12033.82Wv169/000|!('d%*|"
MatchCollection matches = rgx.Matches(TS);
matches.Count 最终为 1,matches[0] 为“|!('d%*|”。
我原以为 matches.Count 是 3,解析后的字符串是:
matches[0] = "!("
matches[1] = "'d"
matches[2] = "%*"
我做错了什么?
查克
最佳答案
您的正则表达式将 |
条之间的所有内容捕获到一个匹配项中。如果您想要括号中的部分,它们位于 match[0].Groups
中。
Group[0]
是整个捕获组。第 1 组、第 2 组以及可选的第 3 组和更多组将成为括号中的字符对。
在您的情况下,matches.Count 将为 1,matches[0].Groups.Count 将为 4,其中:
matches[0].Group[1] == "!("
matches[0].Group[2] == "'d"
matches[0].Group[3] == "%*"
关于c# - 当我期待 3 时只得到一场比赛,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12414735/