-2

I have this code that I need to parse the process ID from text:

@Test
public void testParseProcessId() {
    String text = "Private property of Exodus: 1016@localhost";
    Pattern pattern = Pattern.compile("^Private property of Exodus:\\s(\\d+)");
    String matched = null;
    Matcher matcher = pattern.matcher(text);
    if (matcher.matches()) {
        matched = matcher.group(1);
    }
    assertEquals("1016", matched);
}

The test fails, but I have checked the the regex should be correct, what am I missing here?

Fireburn
  • 981
  • 6
  • 20

1 Answers1

0

The method matches() returns only true if the whole string can be matched.

You should change the regex: ^Private property of Exodus:\\s(\\d+).*$ to get a full match. This way matcher.matches() results true

Hülya
  • 3,353
  • 2
  • 12
  • 19