I need to find specific word on a string using regular expression. rules : before that word, I must not have a letter, a number or an underscore. this word must be not followed by a letter, a number or an underscore.
Example : I'm looking for the word 'the' in : 'Hi, it\'s a random text containing some words, the .the thingthe THE, tthe Tthe [email protected]'
in this string It must be found 3 times : the and .the and THE so I must get true as a return.
my code :
String str = 'Hi, it\'s a random text containing some words, the .the thingthe THE, tthe Tthe [email protected]';
String var = 'the';
String s = '(?<![\\w])'+var.toUpperCase()+'(?![\\w])';
Pattern MyPattern = Pattern.compile(s);
Matcher MyMatcher = MyPattern.matcher(str.toUpperCase());
System.debug('###matches : '+MyMatcher.matches());
Matches return always false.
I tried with this also :
'(?<![a-zA-Z0-9_])'+var.toUpperCase()+'(?![a-zA-Z0-9_])';