0
  • Platform: Java
  • Current pattern:(/[^:" ()]+\..[^: "()]+).*(\d.*?):(\d.*)?.*?((?:warning|error|note): .+)
  • Problem: for group 4, it only matches to the end of line. If I add DOTALL it finds only one set of groups with the description of the last find.

Input for which I want to find a match - the one that goes into pattern.compile(String):

Build failed: Command failed with exit code 1.
stderr: /sample/path/to/SampleFile.java:1: error: [PackageLocation] Expected package /sample/path/to/ to be declared in a directory ending with /sample/path/to, instead found /sample/path/To
package sample.path.to;
                   ^
    (see http://errorprone.info/bugpattern/PackageLocation)
/sample/path/to/SampleFile2.java:-1: note: Some input files use or override a deprecated API.

/sample/path/to/SampleFile2.java:-1:6 note: Recompile with -Xlint:deprecation for details.

Expected output:

Find 0

  • group 1: /sample/path/to/SampleFile.java
  • group 2: 1
  • group 3: null
  • group 4: error: [PackageLocation] Expected package /sample/path/to/ to be declared in a directory ending with /sample/path/to, instead found /sample/path/To; ^ (see http://errorprone.info/bugpattern/PackageLocation)

Find 1

  • group 1: /sample/path/to/SampleFile2.java
  • group 2: 1
  • group 3: null
  • group 4: note: Some input files use or override a deprecated API.

Find 2

  • group 1: /sample/path/to/SampleFile2.java
  • group 2: 1
  • group 3: 6
  • group 4: note: Recompile with -Xlint:deprecation for details.
Humble Student
  • 2,913
  • 3
  • 15
  • 31

1 Answers1

1

Code

Making some slight modifications to your regex, you can use this.

See regex in use here

(/[^:" ()]+\..[^: "()]+):-?(\d.*?):(\d.*)?.*?((?:warning|error|note): (?:(?![\r\n]{2})[\s\S])+)
                        ^^^                                           ^^^^^^^^^^^^^^^^^^^^^^^^

Explanation

Instead of counting on the end of the line to determine the end of the match, I decided to use a double newline character (since this seems to be a common delimiter between the 3 lines). I'll focus on explaining the parts I added/modified.

  1. I changed .* to :-?
    • While this may be a small change, it could potentially help you to not catch incorrect parts of the string in the future.
  2. (?:(?![\r\n]{2})[\s\S])+ Match the following one or more times (tempered greedy token)
    • (?![\r\n]{2}) Negative lookahead ensuring what follows is not exactly two newline characters
    • [\s\S] Match any character (including newline characters)
ctwheels
  • 19,377
  • 6
  • 29
  • 60