Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 每行有多个匹配集合_C#_.net_Regex - Fatal编程技术网

C# 每行有多个匹配集合

C# 每行有多个匹配集合,c#,.net,regex,C#,.net,Regex,我正在逐行解析一个文本文件,如何创建多个匹配集合并在处理的每一行上尝试多个匹配 目前我正在尝试: while ((line = reader.ReadLine()) != null) { string match1 = @"\s+([^)]*):entry:\s+cust\(([^)]*)\)\s+custno\(([^)]*)\)\s+id\(([^)]*)\)\s+name\(([^)]*)\)"; string match2 = @"group\(([^)]*)\)\s+spec\(([

我正在逐行解析一个文本文件,如何创建多个匹配集合并在处理的每一行上尝试多个匹配

目前我正在尝试:

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美元。为什么我的最后2个ArrayList是空的?应该有很多匹配项,正则表达式已确认正确

样本数据:

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)

如果要在数据上使用文字,则必须在第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)

因为第二个和第三个正则表达式根本不匹配?不知道你的台词是什么样子,我们不能给你一个明确的答复。您是否查看了阅读器和集合的内容,以验证您是否得到了预期的结果?为什么要使用ArrayList?请确保在第一次调用Regex.Matches后,该行中仍然包含内容。我在API上注意到的另一件事是“集合只包括成功的匹配,并在第一次不成功的匹配时终止。”。。该方法是否可以提前终止?你能给我们你正在测试的文件吗。。。。ref:@Jeff确认了regexbuddy中的regex,并使用了相关的行。文件包含机密客户数据,我将发布示例数据。@一些随机的孩子-不,最后2个正则表达式有问题。