How to provide replaceAll will replace the whole word, not a substring

I have a dictionary. The dictionary is repeated to replace the key text with the dictionary in the text. But the replaceAll function also replaces subString .

How to make sure it matches the whole word (in general, 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()); } 
+5
source share
1 answer

replaceAll accepts a regular expression as a parameter.

In regular expressions, you have word boundaries : \b (use \\b in a string literal). This is the best way to match a word, not a part of a word: "\\bword\\b"

But in your case, you cannot use word boundaries because you are not looking for a word ( [69-3] not a word).

I suggest the following:

 text=text.replaceAll("(?=\\W+|^)"+Pattern.quote("[69-3]")+"(?=\\W+|$)", ... 

The idea is to match the end of a line or something that is not a word. I can not guarantee that this will be the right solution for you: such a template should be customized, knowing the exact full use case.

Please note that if all your keys correspond to a similar pattern, there may be a better solution than repeating iteration through the dictionary, you can use, for example, a pattern, for example "(?=\\W+|^)\\[\\d+\\-\\d+\\](?=\\W+|$)" . "(?=\\W+|^)\\[\\d+\\-\\d+\\](?=\\W+|$)"

+7
source

Source: https://habr.com/ru/post/1202127/


All Articles