Java Regex: match a whole word with a word

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.

+5
source share
2 answers

It looks like you want to combine “words” enclosed in spaces (or at the beginning / end of lines).

Using

 String pattern = "(?<!\\S)" + Pattern.quote(word) + "(?!\\S)"; 

A negative lookbehind (?<!\S) will not be able to fulfill all matches preceded by a char other than a space, and (?!\s) a negative result that will not be able to fulfill all matches followed by a char, except whitespace. Pattern.quote() you must avoid special characters that must be treated as alphabetic characters in the regular expression pattern.

+5
source

Try to avoid special characters with backslashes. They may have other meanings in the template.

slight correction: maybe you even need a double backslash, since the backslash is a special character in the string.

0
source

Source: https://habr.com/ru/post/1265703/


All Articles