Harry
Harry

Reputation: 23

Uses matches to match a whole word

Researching another post, I found a good way of matching on a whole word.
The following matches for the hardcoded word "the"

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

I want to make this a little more complicated. I want to match on the word that the user enters.
So if I setup

String userInput;

and validate it etc. etc.
How do I modify the above matches so it validates on the whole word that is contained in the String userInput?
Thanks

Upvotes: 0

Views: 61

Answers (1)

peter.petrov
peter.petrov

Reputation: 39437

The obvious approach is to try this. I think it will work.

System.out.println(text.matches(".*\\b" + userInput + "\\b.*"));

Upvotes: 3

Related Questions