2016-05-14 142 views
0

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

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次。

回答

1

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

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

实例它的运作方式使用Perl的例子(正则表达式是相同的)

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

嘿马辛,是我能够扫描每行,并使用正则表达式'输出。*( [^] + \(\))。*没有部署。*',我接近我想要的输出。我得到这个'com。somename.qa.mobile.tests.somename.SomeTest.testSomeName()'就像你刚才提到的那样。有没有一种方法可以摆脱最初的'com.somename.qa.mobile.tests.'部分(这是我的原始问题),只是打印'somename.SomeTest.testSomeName()'。这部分我想进一步删除将每次都是相同的。 –

+1

当然,你可以把这个部分放在mateched表达式之前,例如。 '/com.somename.qa.mobile.tests.([^] + \(\))。* not deployed /' –

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

这是一篇很棒的文章! – erip

+1

太棒了!我从这个答案中学到了很多东西! –