在c#中使用多行和分组的正则表达式

时间:2017-05-18 13:12:59

标签: c# regex regex-group

我正在努力使用正文下面的正则表达式

30T1 Error: NIO from  machine - sequence Nr. 1456
30T1 Error: Failure from Stud machine point Nr. 100
30T1 Warning: Stop at point not possible 
30T1 Error " 10" FS130 Machine stops working
30T1 Error " 331"  : Motor not working!
30T1 Error " 331"  Stud Machine Failure: Motor not working!
30T1 Error " 331"  Stud Machine Failure: Motor not working!
30T1 Warning: " 11" FS130  working
30T1 Error: NIO from machine - sequence Nr. 1356
30T1 Warning: Stop at point not possible 
30T1 Error: Failure from Stud machine point Nr. 110
30T1 Error " 331"  Machine: not available!
30T1 Error " 10" FS130  stops working
30T1 Error " 332"  Stud Machine Failure: current not available!

我想匹配这样的输出并进行分组

30T1 Error: NIO from  machine - sequence Nr. 1456
30T1 Error: Failure from Stud machine point Nr. 100
30T1 Error " 331"  Stud Machine Failure: Motor not working!

30T1 Error: NIO from machine - sequence Nr. 1356
30T1 Error: Failure from Stud machine point Nr. 110
30T1 Error " 332"  Stud Machine Failure: current not available!

直到现在我想出了以下正在使用的正则表达式,但它只显示了我的第一行。我无法获得可用于多行的正则表达式。

正则表达模式我现在尝试单位:

(?<Error1>30T1\s+\w.+.?sequence\s+Nr.\s[0-9]+)

我不是正则表达式的专家,我希望有人可以启发我制作更好的正则表达式。

1 个答案:

答案 0 :(得分:1)

建议使用IEnumerable<string> Linq 而不是正则表达式

private static IEnumerable<string[]> ExtractLines(IEnumerable<string> source) {
  List<string> group = new List<string>();

  foreach (var line in source) {
    if (line.Contains("machine - sequence Nr.")) {
      if (group.Any())
        yield return group.ToArray();

      group.Clear();
      group.Add(line);
    }
    else if (line.Contains("Stud machine point Nr.") || 
             line.Contains("Stud Machine Failure"))
      if (!group.Contains(line)) // remove duplicates
        group.Add(line);
  }

  if (group.Any())
    yield return group.ToArray();
}

private static IEnumerable<String> MakeReport(IEnumerable<string> source) {
  bool firstLine = true;

  foreach (var group in ExtractLines(source)) {
    if (!firstLine)
      yield return "";

    firstLine = false;

    yield return (string.Join(Environment.NewLine, group));
  }
}

测试:

string text =
@"30T1 Error: NIO from  machine - sequence Nr. 1456
30T1 Error: Failure from Stud machine point Nr. 100
30T1 Warning: Stop at point not possible
30T1 Error "" 10"" FS130 Machine stops working
30T1 Error "" 331""  : Motor not working!
30T1 Error "" 331""  Stud Machine Failure: Motor not working!
30T1 Error "" 331""  Stud Machine Failure: Motor not working!
30T1 Warning: "" 11"" FS130 working
30T1 Error: NIO from machine - sequence Nr. 1356
30T1 Warning: Stop at point not possible
30T1 Error: Failure from Stud machine point Nr. 110
30T1 Error "" 331""  Machine: not available!
30T1 Error "" 10"" FS130 stops working
30T1 Error "" 332""  Stud Machine Failure: current not available!";

...

  var lines = text
    .Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

  var filtered = MakeReport(lines);

  foreach (var line in filtered) 
    Console.WriteLine(line);

结果:

30T1 Error: NIO from  machine - sequence Nr. 1456
30T1 Error: Failure from Stud machine point Nr. 100
30T1 Error " 331"  Stud Machine Failure: Motor not working!

30T1 Error: NIO from machine - sequence Nr. 1356
30T1 Error: Failure from Stud machine point Nr. 110
30T1 Error " 332"  Stud Machine Failure: current not available!

我猜你实际上使用的是log 文件,在这种情况下:

  var filtered = MakeReport(File
    .ReadLines(@"C:\MyLog.log"));

  foreach (var line in filtered) 
    Console.WriteLine(line);