5

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

So, if I'm trying to find the word "the" in a sentence, it should return true for "the quick brown fox jumps over the lazy dog", but return false for "there quick brown fox jumps over the lazy dog".

I've tried this:

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

I've 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've also tried this regex: "\bthe\b"

And they always return false. I feel like I'm missing something pretty obvious here, since this shouldn't be too difficult. :)

Kris B
  • 3,436
  • 9
  • 64
  • 106

4 Answers4

8

If you use matches, it must match the whole String. String#contains(...) may be what you're looking for, or perhaps you want to put some wild cards before and after your word:

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

e.g.,

  String text = "the quick brown fox jumps over the lazy dog";
  System.out.println(text.matches(regex));
DrB
  • 264
  • 1
  • 3
  • 14
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
6

What you need is matcher.find()

http://download.oracle.com/javase/1.5.0/docs/api/java/util/regex/Matcher.html#find%28%29

3

Try this regex:

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

Basically, what you are trying to match it against is the string "the", but you need to return a match even if the input has other things, so you need to put ".*" on both sides for that.

Hari Menon
  • 33,649
  • 14
  • 85
  • 108
0

Bzzt! It will naturally return true for the second sentence, since the word the appears two times: The quick brown fox jumps over the lazy dog". Your second sentence still has the second the.

Your regex is correct. However, you'll need to use the Pattern.find method instead of matches, because matches attempts to match against the whole string. Pattern.find, on the other hand, will locate substrings that match your pattern.

zneak
  • 134,922
  • 42
  • 253
  • 328