Full word Regex

I feel a little silly asking this question, but from everything I read, this should work, but for me it is not. I'm just trying to match a whole word in a string using regular expressions.

So, if I try to find the word “the” in a sentence, it should return true for “fast brown fox jumping over a lazy dog,” but return the lie for “there fast brown fox jumping over a lazy dog”.

I tried this:

 String text = "the quick brown fox jumps over the lazy dog";
 return text.matches("\\bthe\\b");

I also tried:

    String text = "the quick brown fox jumps over the lazy dog";
    String regex = "\\bthe\\b";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(text);

    return matcher.matches();

I also tried this regex: "\ bthe \ b"

And they always return false. I feel like I'm missing something pretty obvious here, as it shouldn't be too complicated. :)

+5
4

matches, . String#contains(...) , , , , :

String regex = ".*\\bthe\\b.*";

.

  String text = "the quick brown fox jumps over the lazy dog";
  System.out.println(text.matches(regex));
+5

:

".*\\bthe\\b.*"

, , , "the", , , ". *" .

+1

Bzzt! , true , the : ". .

. Pattern.find matches, matches . Pattern.find, , , .

0

All Articles