Home > Blockchain >  Regex to capture Maven test output
Regex to capture Maven test output

Time:02-28

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?

CodePudding user response:

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

  • Related