How can we do a keyword search with special characters in a string with pattern and matcher class I used similar code as below to do the same. However this does not work if there are special characters like ( , ), - present in the keyword. I tried using \w to the code but it did not work.
static Boolean keywordSearch(String input, String keywordPhrase)
{
return Pattern.compile('(?i)\\b\\w' + keywordPhrase + '\\b\\w').matcher(input).find();
}
String corpus = 'The quick brown fox jumps over the lazy (dog)';
system.assert(keywordSearch(corpus, 'lazy (dog)')); //Fails and it is not detected
system.assert(keywordSearch(corpus, 'QUICK')); // Pass
system.assert(!keywordSearch(corpus, 'jump')); //Pass