11
I have list of keywords entered by the user and they may contains the special characters like $, #, @, ^, &, etc.
As per my requirement when ever i receive list of text messages i need to search for all the keywords in every message.
We need to match exact keyword.
CASE 1: Simple Keyword - Simple Message
I used \b to match exact keyword and it works fine.
public static void main(String[] args) {
String patternStr = "(?i)\\bHello\\b";
Pattern pattern = Pattern.compile(patternStr);
List<String> strList = new ArrayList<String>();
strList.add("HHello Message");
strList.add("This is Hello Message ");
strList.add("Now Hellos again.");
for(String str : strList) {
Matcher matcher = pattern.matcher(str);
System.out.println(">> "+matcher.find());
}
}
OUTPUT as Expected
>> false
>> true
>> false
CASE 2 : Simple Keyword - Message with Special Character
Now, if i run above same code for following messages then it didn't work as expected.
List<String> strList = new ArrayList<String>();
strList.add("#Hello Message");
strList.add("This is Hello Message ");
strList.add("Now Hellos again.");
OUTPUT:
true
true
false
Expected OUTPUT
false
true
false
CASE 3 : Keyword & Message with Special Character
If i receive following messages and Keyword is #Hello.
I wrote following code but it didn't work.
public static void main(String[] args) {
String patternStr = "