用于在Jenkins控制台日志中查找特定文本的正则表达式

时间:2016-05-14 23:20:08

标签: python regex

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

somename.SomeTest.testSomeName()

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

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.......

上述句子在控制台日志中出现超过20次。我想找到每个实例 这个并获得该方法名称somename.SomeTest.testSomeName()。到目前为止,我已经尝试了\[([^\]]+)\] not deployed ([^ ]+)等等 作为此正则表达式的一部分缺失/不正确

以下是日志的小片段,我想从下面显示的每一个中找出test method name     Failed 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次。

2 个答案:

答案 0 :(得分:2)

描述

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

Regular expression visualization

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

  • 找到[error]
  • 的所有行
  • 捕获方法名称,它是()
  • 之前的三个文本块

实施例

直播示例

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

示例文字

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.alpha.SomeTest.testSomeName(). The result is not deployed to Platform but we will proceed with further tests
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(). The result is not deployed to Platform but we will proceed with further tests
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(). 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

样本匹配

[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

答案 1 :(得分:1)

我假设您可以扫描每行输出。试试这个正则表达式

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

示例如何使用Perl(regexp是相同的)

的示例
$ cat example | perl -ne 'print $1 if /([^ ]+\(\)).*not deployed/;'
com.somename.qa.mobile.tests.somename.SomeTest.testSomeName()