I have an input of dictionary. The dictionary is iterated over to replace the key
from dictionary in the text. But replaceAll
function replaces the subString
as well.
How to ensure that it will match the whole word (as a whole and not as a subString
)
String text= "Synthesis of 1-(2,6-dimethylbenzyl)-1H-indole-6-carboxylic acid [69-3] The titled compound (883 mg) sdvfshd[69-3]3456 as a white solid was prepared"
dictionary= {[69-3]=1-(2,6-dimethylbenzyl)-1H-indole-6-carboxylic acid }
for(Map.Entry<String, String> entry : dictionary.entrySet()){
text=text.replaceAll("\\b"+Pattern.quote(entry.getKey())+"\\b", entry.getValue());
}
equal
before replacing? – Tav