我正在尝试在文本/.log文件中搜索关键字" DEBUG 2018-06-04T13:27:15 + 00:00" 一旦找到最后一个关键词DEBUG,我就想将日期和时间与当前日期$ time进行比较,如果超过10分钟输出失败。
答案 0 :(得分:2)
您可以使用Regex.Matches
提取所有匹配的子字符串:
String text = File.ReadAllText(@"C:\My\File\Path.txt");
MatchCollection matches = Regex.Matches(
text,
@"DEBUG (\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+\d{2}:\d{2})"
)
检查最后一场比赛:
if (matches.Count > 0) {
Match last = matches[matches.Count - 1];
DateTime dt = DateTime.Parse(last.Groups[1].Value);
if (DateTime.Now > dt.AddMinutes(10)) {
Console.WriteLine("Last date entry is older than 10 minutes ago: {0}", dt);
}
}
或循环完成所有比赛:
foreach (Match match in matches) {
// Parse the date string in the matched text:
DateTime dt = DateTime.Parse(match.Groups[1].Value);
if (DateTime.Now > dt.AddMinutes(10)) {
// Date is older than 10 minutes ago
Console.WriteLine("Date is older than 10 minutes ago: {0}", dt);
}
}