Search code examples
javaregexstring-formatting

Java Regex Matcher Problem with dynamic Strings


I have some problems with Regex in Java and dynamic input - No problems with Regex at all ;)

private static Pattern START_SUITE = Pattern.compile("Test Suite '(\\S+)'.*started at\\s+(.*)");

String line = "Test Suite '/a/long/path/to/some/file.octest(Tests)' started at 2011-07-09 08:01:34 +0000";

Matcher m = START_SUITE.matcher(line);

if (m.matches) {
    //do something
}

This works fine with my test java application with the string above. But when the String does come from an other source Matcher doesn't match it.

processHandler.addProcessListener(new ProcessAdapter() {
 @Override
 public void onTextAvailable(final ProcessEvent event, final Key outputType) {
 try {
   outputParser.myMatchStringFunction(event.getText());
 }
 ...
}

 public void myMatchStringFunction(String line) {
  Matcher m = START_SUITE.matcher(line);
  if (m.matches) {
  ...

I checked the String with printing and it looks ok.

Any ideas what could happen?


Solution

  • Thanks for that hint.

    Adding DOTALL and (.*) at the end of every pattern solved the problem

    private static Pattern START_SUITE = Pattern.compile("Test Suite '(\\S+)'.*started at\\s+(.*)", Pattern.DOTALL);