【发布时间】:2025-12-15 10:10:02
【问题描述】:
我有一个字典输入。迭代字典以替换文本中字典中的key。但是replaceAll 函数也替换了subString。
如何确保匹配整个单词(作为一个整体而不是作为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的元素? -
我没有得到这个问题..你所说的平等是什么意思???我正在使用整个文本来替换它并且没有标记它..
标签: java regex replaceall