用于在Jenkins控制台日志中查找特定文本的Regex

2024-09-28 22:20:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写Python脚本来过滤Jenkins控制台日志中的Jenkins作业,我想用正则表达式搜索特定的模式/文本。 以下是我要查找的文本:

somename.SomeTest.testSomeName()

以下段落或句子的一部分:

^{pr2}$

以上这些句子在控制台日志中出现了20多次。我想找到 然后得到方法名somename.SomeTest.testSomeName()。到目前为止,我已经尝试过了 缺少/不正确作为此正则表达式的一部分

下面是日志的小片段,我想从下面显示的每个日志中找出test method nameFailed to find the annotation....句子:

21:18:19    at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:170)
21:18:19    at org.apache.maven.surefire.testng.TestNGXmlTestSuite.execute(TestNGXmlTestSuite.java:84)
21:18:19    at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:92)
21:18:19    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:200)
21:18:19    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:153)
21:18:19    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)
21:18:26 2016-05-12 21:18:25.238 [ERROR] (1): Failed to find the annotation and the status of the test public void com.somename.qa.mobile.tests.somename.SomeTest.testSomeName(). The result is not deployed to Platform but we will proceed with further tests
21:18:26 somename.client.test.utilities.Platform.PlatformApiException: Platform API returned HTTP 400("Field :case_id is not a valid test case.")
21:18:26    at somename.client.test.utilities.platform.PlatformApiClient.sendRequest(PlatformApiClient.java:197)
21:18:26    at

上面只是一个小片段;想象一下上面的片段在日志中出现了20多次。在


Tags: thetoorgtestapachejavaat句子
2条回答

我想你可以扫描每行的输出。试试这个regexp

.* ([^ ]+\(\)).*not deployed.*

示例如何使用Perl(regexp是相同的)与您的示例一起工作

^{pr2}$

说明

^.*?(\[Error\]).*?\.((?:[a-z]+\.){2}[a-z]+\(\))

Regular expression visualization

此正则表达式将执行以下操作:

  • 使用[error]查找所有行
  • 捕获方法名,它是()前面的三个文本块

示例

现场示例

https://regex101.com/r/kI4cL2/1

示例文本

^{pr2}$

样本匹配

[0][0] = 21:18:26 2016-05-12 21:18:25.238 [ERROR] (1): Failed to find the annotation and the status of the test public void com.somename.qa.mobile.tests.alpha.SomeTest.testSomeName()
[0][1] = [ERROR]
[0][2] = alpha.SomeTest.testSomeName()

[1][0] = 21:18:26 2016-05-12 21:18:25.238 [ERROR] (1): Failed to find the annotation and the status of the test public void com.somename.qa.mobile.tests.bravo.SomeTest.testSomeName()
[1][1] = [ERROR]
[1][2] = bravo.SomeTest.testSomeName()

[2][0] = 21:18:26 2016-05-12 21:18:25.238 [ERROR] (1): Failed to find the annotation and the status of the test public void com.somename.qa.mobile.tests.charlie.SomeTest.testSomeName()
[2][1] = [ERROR]
[2][2] = charlie.SomeTest.testSomeName()

解释

NODE                     EXPLANATION
                                   
  ^                        the beginning of a "line"
                                   
  .*?                      any character except \n (0 or more times
                           (matching the least amount possible))
                                   
  (                        group and capture to \1:
                                   
    \[                       '['
                                   
    Error                    'Error'
                                   
    \]                       ']'
                                   
  )                        end of \1
                                   
  .*?                      any character except \n (0 or more times
                           (matching the least amount possible))
                                   
  \.                       '.'
                                   
  (                        group and capture to \2:
                                   
    (?:                      group, but do not capture (2 times):
                                   
      [a-z]+                   any character of: 'a' to 'z' (1 or
                               more times (matching the most amount
                               possible))
                                   
      \.                       '.'
                                   
    ){2}                     end of grouping
                                   
    [a-z]+                   any character of: 'a' to 'z' (1 or more
                             times (matching the most amount
                             possible))
                                   
    \(                       '('
                                   
    \)                       ')'
                                   
  )                        end of \2

相关问题 更多 >