我正在逐行解析文本文件,如何创建多个匹配集合并在处理的每一行上尝试多个匹配?
目前我正在尝试这个:
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);
}
}
处理完文件并尝试计算arraylist大小后:
MessageBox.Show(line1.Count + " " + line2.Count + " " + line3.Count);
我收到5000 0 0.为什么我的最后2名arraylists为空?应该有很多匹配,正则表达式确认正确。
示例数据:
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)
答案 0 :(得分:0)
如果您要对数据进行文字处理,则必须在第2和第3个正则表达式中要求缺少组件。或者,如果是变量,只需添加一些填充.*?
group
\(
( [^)]* )
\)
\s+ spec
\(
( [^)]* )
\)
# missing :
# \s+ stripdn
# \(
# ( [^)]* )
# \)
# \s+ pre228
# \(
# ( [^)]* )
# \)
\s+ goodtill
( [^)]* )
$
returns
# missing a \s* here
\(
( [^)]* )
\)
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)