I am trying to check if a string contains a word in its entirety using Java. The following are some examples:
Text : "A quick brown fox" Words: "qui" - false "quick" - true "quick brown" - true "ox" - false "A" - true
Below is my code:
String pattern = "\\b(<word>)\\b"; String s = "ox"; String text = "A quick brown fox".toLowerCase(); System.out.println(Pattern.compile(pattern.replaceAll("<word>", s.toLowerCase())).matcher(text).find());
It works great with strings like the one I mentioned in the above example. However, I get incorrect results if the input line contains characters like % , ( etc., for example:
Text : "c14, 50%; something (in) bracket" Words: "c14, 50%;" : false "(in) bracket" : false
This has something to do with my regex pattern (or maybe I'm mistakenly doing the whole pattern). Can anyone suggest me a better approach.
source share