正则表达式将字符串中的日期与 where 子句匹配

我需要从下面的示例文本中获取日期字符串,即2019-01-22 15:36:141,023 ,仅当该行包含Correct而不是Test12单词时。所以理想情况下,我应该在下面的字符串中得到两个匹配项(第 3 行和第 5 行)。

第 1 行:2019-01-22 15:36:141,043:[测试][123] 信息 - 测试:正确的 Test12 ping

第 2 行:2019-01-22 15:36:141,029:[测试][124323] 信息 - 测试:错误的 Test12 ping

第 3 行:2019-01-22 15:36:141,023:[测试][12554363] 信息 - 测试:正确测试 ping

第 4 行:2019-01-22 15:36:141,123: [Test][6761213] INFORMATION - Testing: Wrong Test12 ping

第 5 行:2019-01-22 15:36:141,093:[测试][46543123] 信息 - 测试:测试 ping 无效

第 6 行:2019-01-22 15:36:141,890:[测试][887] 信息 - 测试:正确测试 ping

我可以用(?\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}(?:,\ d{3}\b)?)但不确定如何包含其他条件。任何线索?


波斯汪
浏览 71回答 3
3回答

倚天杖

在不增加 regex 额外复杂性的情况下,您可以遍历文件中的行并执行检查Test12并Correct使用常规字符串方法:var results = new List<string>();using (var sr = new StreamReader(filepath, true))&nbsp;{&nbsp; &nbsp; var line = "";&nbsp; &nbsp; while ((line=sr.ReadLine()) != null)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (line.Contains("Correct") && !line.Contains("Test12"))&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var res = Regex.Match(line, @"\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2,}(?:,\d{3}\b)?");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (res.Success)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; results.Add(res.Value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}使用正则表达式,如果要检查是否存在的单词出现在使用日期之后\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2,}(?:,\d{3}\b)?(?!.*Test12)(?=.*Correct)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^^^^^^^^^^^^^^^^^^^^^^^^^请参阅正则表达式演示。在这里,(?!.*Test12)(?=.*Correct)先行确保 1) 没有Test12和 2)Correct在当前位置右侧(即日期之后)除换行符之外的任何 0+ 个字符之后有一个子字符串。如果这些词可能出现在字符串中的任何位置,您可以使用(?m)\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2,}(?:,\d{3}\b)?(?=.*\r?$(?<!Test12.*)(?<=Correct.*))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^请参阅此正则表达式演示。在这里,该(?m)选项将 设置RegexOptions.Multiline为true以便$可以将其解析为行尾锚点,并且(?=.*\r?$(?<!Test12.*)(?<=Correct.*))正向先行执行以下检查:它要求到行尾为止有 0+ 个字符,然后,在在该行的末尾,这两个检查是使用后视执行的:负后视(?<!Test12.*)确保一行中没有Test12 任何地方,而正后视(?<=Correct.*)确保一行中的任何地方Correct都有子字符串。可选的\r?CR before$是必需的,因为一个相当烦人的事实是在多行模式下$不匹配 before \r。

ibeautiful

我认为您的意思是第 3 行和第 6 行匹配,因为第 5 行不包含Correct.要不包含“Test12”,您可以使用否定前瞻。要在之后匹配“正确”,您可以在您的模式中匹配它并使用单词边界\b来防止它成为更大单词的一部分。您的模式可能如下所示:^(?!.*\bTest12\b).*?(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2,}(?:,\d{3}\b)?).*\bCorrect\b.*$这将匹配:^字符串的开始(?!.*\bTest12\b)断言以下内容不包含Test12.*?匹配任何非贪婪的字符(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2,}(?:,\d{3}\b)?)在一个组中捕获像模式一样的日期.*匹配任意字符 0+ 次\bCorrect\b匹配正确.*匹配任意字符 0+ 次$字符串的结尾正则表达式演示|&nbsp;C# 演示笔记这部分是否也应该像查看示例数据(?:,\d{3}\b)?一样匹配逗号前的数字?(?:\d,\d{3}\b)?

函数式编程

这是没有正则表达式的一种方法。日期看起来不正确。我认为逗号位置错误,所以我修复了它。&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DateTime today = DateTime.Parse("2019-01-22 15:36:14");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string input =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "2019-01-22 15:36:14,1023: [Test][123] INFORMATION - Testing: Correct Test12 ping\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "2019-01-22 15:36:14,1023: [Test][124323] INFORMATION - Testing: Wrong Test12 ping\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "2019-01-22 15:36:14,1023: [Test][12554363] INFORMATION - Testing: Correct Test ping\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "2019-01-22 15:36:14,1023: [Test][6761213] INFORMATION - Testing: Wrong Test12 ping\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "2019-01-22 15:36:14,1023: [Test][46543123] INFORMATION - Testing: Invalid Test ping\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "2019-01-22 15:36:14,1023: [Test][887] INFORMATION - Testing: Correct Test ping";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StringReader reader = new StringReader(input);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string line = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ((line = reader.ReadLine()) != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string[] splitDate = line.Split(new string[] { ": [Test]" }, StringSplitOptions.None);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DateTime date = DateTime.ParseExact(splitDate[0].Replace(",","."), "yyyy-MM-dd HH:mm:ss.FFFF", System.Globalization.CultureInfo.InvariantCulture);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string[] splitTest = splitDate[1].Split(new char[] { ':' });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((date.Date == today.Date) && splitTest[1].Contains("Correct") && !splitTest[1].Contains("Test12"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(line);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.ReadLine();
打开App,查看更多内容
随时随地看视频慕课网APP