用于捕获 Maven 测试输出的正则表达式

发布于 2025-01-10 12:26:31 字数 921 浏览 0 评论 0原文

当我运行测试时,我有这个 Maven 输出:

...
[INFO] 
[ERROR] Tests run: 77, Failures: 0, Errors: 2, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  42:09 min
[INFO] Finished at: 2022-02-25T10:40:21Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M5:test (default-test) on project ***-***-******-*****: There are test failures.
[ERROR]
...

我想用 Regex 或其他方式捕获这一行:

Tests run: 77, Failures: 0, Errors: 2, Skipped: 0

我不需要准确地捕获它,但至少需要捕获一般区域,因为每个测试类都会生成自己的测试摘要 由于

到目前为止,我想出的正则表达式是:

((?<=\[ERROR\]).*\n).*((?=BUILD).*\n)

某种原因,它不起作用。

我该怎么做?

I have this Maven output when I run my tests:

...
[INFO] 
[ERROR] Tests run: 77, Failures: 0, Errors: 2, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  42:09 min
[INFO] Finished at: 2022-02-25T10:40:21Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M5:test (default-test) on project ***-***-******-*****: There are test failures.
[ERROR]
...

I want to capture this line with Regex or otherwise:

Tests run: 77, Failures: 0, Errors: 2, Skipped: 0

I don't need to capture it exactly, but at least the general area, as each test class produces its own test summary of tests run, passed etc.

The RegEx I have come up with so far is:

((?<=\[ERROR\]).*\n).*((?=BUILD).*\n)

Which doesn't work for some reason.

How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸢与 2025-01-17 12:26:31

您可以

\[ERROR](.*)(?:\n(?!.* BUILD\b|\[ERROR]).*)*\n.* BUILD\b

在部分中使用模式匹配:

  • \[ERROR\] 匹配 [ERROR]
  • (.*) 捕获组 1,匹配整行
  • (?: 非捕获组
    • \n 匹配换行符
    • (?! 断言右侧的内容不是负向前瞻
      • .* BUILD\b 匹配行中的 BUILD
      • | 或者
      • \[ERROR] 匹配 [ERROR]
    • ) 关闭前瞻
    • .* 匹配整行
  • )* 关闭非捕获组并可选择重复
  • \n .* BUILD\b 匹配换行符并 的行

BUILD Regex demo

You could use

\[ERROR](.*)(?:\n(?!.* BUILD\b|\[ERROR]).*)*\n.* BUILD\b

In parts, the pattern matches:

  • \[ERROR\] Match [ERROR]
  • (.*) Capture group 1, match the whole line
  • (?: Non capture group
    • \n Match a newline
    • (?! A negative lookahead to assert what is to the right is not
      • .* BUILD\b Match BUILD in the line
      • | Or
      • \[ERROR] Match [ERROR]
    • ) Close the lookahead
    • .* Match the whole line
  • )* Close non capture group and optionally repeat
  • \n.* BUILD\b Match a newline and the line with BUILD

Regex demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文