If you want to match a word in a sentence even with punctuation, you'll need a regex like this:
static boolean matchesWord(String toMatch, String matchIn) {
return Pattern.matches(".*([^A-Za-z]|^)"+toMatch+"([^A-Za-z]|$).*", matchIn);
}
(You could use \W, but that doesn't count underscores as punctuation.)
Just concatenating spaces onto the beginning and the end won't match, for example, the word "programmer" in the string "I am a Java programmer" because there's no space at the end. It also won't match words directly before or after punctuation.