我正在逐行解析文本文件,如何创建多个匹配集合并在处理的每一行上尝试多个匹配?
目前我正在尝试这个:
while ((line = reader.ReadLine()) != null) {
string match1 = @"\s+([^)]*):entry:\s+cust\(([^)]*)\)\s+custno\(([^)]*)\)\s+id\(([^)]*)\)\s+name\(([^)]*)\)";
string match2 = @"group\(([^)]*)\)\s+spec\(([^)]*)\)\s+goodtill([^)]*)$";
string match3 = @"returns\(([^)]*)\)";
MatchCollection matches = Regex.Matches(line, match1);
MatchCollection matches2 = Regex.Matches(line, match2);
MatchCollection matches3 = Regex.Matches(line, match3);
foreach (Match matchr1 in matches)
{
line1.Add("Date:" + matchr1.Groups[1].Value + ", Customer:"
+ matchr1.Groups[2].Value + ", CustID:" + matchr1.Groups[3].Value +
", ID:" + matchr1.Groups[4].Value + ", Name:" + matchr1.Groups[5].Value);
}
foreach (Match matchr2 in matches2)
{
line2.Add("Group:" + matchr2.Groups[1].Value + ", Spec:" + matchr2.Groups[2].Value + ", Good Till:" + matchr2.Groups[3].Value);
}
foreach (Match matchr3 in matches3)
{
line3.Add("Returns: " + matchr3.Groups[1].Value);
}
}
处理文件并尝试计算数组列表大小后:
MessageBox.Show(line1.Count + " " + line2.Count + " " + line3.Count);
我收到 5000 0 0。为什么我的最后 2 个数组列表是空的?应该有很多匹配,正则表达式被确认是正确的。
样本数据:
LUCIE:496 27AUG120755:entry: cust(GUIR) custno(j010705) id(293746) name(mike)
LUCIE:496 27AUG120755: group(0000) spec(03) stripdn(N) pre228(N) goodtill 01/MAR/08
LUCIE:496 27AUG120755:getprotcode given (m000029374603MAR08), returns (TUUjFDEO)