簡體   English   中英

正則表達式將字符串中的日期與where子句匹配

[英]Regex to match the date from string with where clause

我只需要從下面的示例文本中獲取日期字符串,即2019-01-22 15:36:141,023 ,其中該行包含正確的單詞而不是Test12單詞。 因此,理想情況下,我應該在下面的字符串中得到兩個匹配項(第3行和第5行)。

第1行:2019年1月22日15:36:141,043:[Test] [123]信息-測試:正確的Test12 ping

第2行:2019年1月22日15:36:141,029:[Test] [124323]信息-測試:Test12 ping錯誤

第3行:2019年1月22日15:36:141,023:[Test] [12554363]信息-測試:正確的測試ping

第4行:2019-01-22 15:36:141,123:[Test] [6761213]信息-測試:Test12 ping錯誤

第5行:2019-01-22 15:36:141,093:[Test] [46543123]信息-測試:無效的測試ping

第6行:2019-01-22 15:36:141,890:[Test] [887]信息-測試:正確的測試ping

我可以使用(?\\ d {4}-\\ d {2}-\\ d {2} \\ s \\ d {2}:\\ d {2}:\\ d {2}(?:,\\ d {3} \\ b)?),但不確定如何包含其他條件。 有線索嗎?

在不增加正則表達式額外復雜性的情況下 ,您可以遍歷文件中的各行,並使用常規字符串方法對Test12Correct進行檢查:

var results = new List<string>();
using (var sr = new StreamReader(filepath, true)) 
{
    var line = "";
    while ((line=sr.ReadLine()) != null)
    {
        if (line.Contains("Correct") && !line.Contains("Test12")) 
        {
            var res = Regex.Match(line, @"\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2,}(?:,\d{3}\b)?");
            if (res.Success)
            {
                results.Add(res.Value);
            }
        }
    }
}

使用正則表達式時,如果您要檢查日期使用是否存在的單詞

\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2,}(?:,\d{3}\b)?(?!.*Test12)(?=.*Correct)
                                                  ^^^^^^^^^^^^^^^^^^^^^^^^^

參見regex演示

在這里, (?!.*Test12)(?=.*Correct)是先行的,可確保1)沒有Test12和2)在除換行符之外的任何0+個字符之后,在右側盡可能多地添加了一個子字符串Correct當前位置的日期(即日期之后)。

如果這些單詞可能出現在字符串中的任何位置,則可以使用

(?m)\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2,}(?:,\d{3}\b)?(?=.*\r?$(?<!Test12.*)(?<=Correct.*))
                                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

請參閱此正則表達式演示

在這里, (?m)選項將RegexOptions.Multiline設置為true,以便將$解析為行錨的結尾,然后將(?=.*\\r?$(?<!Test12.*)(?<=Correct.*))正向超前執行以下檢查:它要求在行尾最多有0+個字符,然后在該行的末尾,使用lookbehinds執行兩次檢查:負向lookbehind (?<!Test12.*)確保Test12 任何地方都沒有Test12 ,而正向looklookbehind (?<=Correct.*)確保一行上任何地方都沒有Correct子字符串。

\\r? 由於在多行模式下$ \\r\\r之前不匹配,因此在$之前需要可選的CR。

我認為您的意思是匹配第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正確\\bCorrect\\b匹配正確
  • .*匹配任何字符0+次
  • $字符串結尾

正則表達式演示 | C#演示

注意

這部分應該是(?:,\\d{3}\\b)? 還要在逗號前匹配一個數字,例如(?:\\d,\\d{3}\\b)? 看示例數據?

這是沒有正則表達式的一種方法。 日期看起來不正確。 我認為逗號位置錯誤,因此我將其修復。

            DateTime today = DateTime.Parse("2019-01-22 15:36:14");
            string input =
                "2019-01-22 15:36:14,1023: [Test][123] INFORMATION - Testing: Correct Test12 ping\n" +
                "2019-01-22 15:36:14,1023: [Test][124323] INFORMATION - Testing: Wrong Test12 ping\n" +
                "2019-01-22 15:36:14,1023: [Test][12554363] INFORMATION - Testing: Correct Test ping\n" +
                "2019-01-22 15:36:14,1023: [Test][6761213] INFORMATION - Testing: Wrong Test12 ping\n" +
                "2019-01-22 15:36:14,1023: [Test][46543123] INFORMATION - Testing: Invalid Test ping\n" +
                "2019-01-22 15:36:14,1023: [Test][887] INFORMATION - Testing: Correct Test ping";

            StringReader reader = new StringReader(input);
            string line = "";

            while ((line = reader.ReadLine()) != null)
            {
                string[] splitDate = line.Split(new string[] { ": [Test]" }, StringSplitOptions.None);
                DateTime date = DateTime.ParseExact(splitDate[0].Replace(",","."), "yyyy-MM-dd HH:mm:ss.FFFF", System.Globalization.CultureInfo.InvariantCulture);
                string[] splitTest = splitDate[1].Split(new char[] { ':' });

                if ((date.Date == today.Date) && splitTest[1].Contains("Correct") && !splitTest[1].Contains("Test12"))
                {
                    Console.WriteLine(line);
                }
            }
            Console.ReadLine();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM