I realize this is an old post, but for others that stumble onto this, another reason you can get this result is if you have incorrect "keys" in your HashMap. So in my case, I was using my old xml format as the key like
.put("<variable/>","TEST");
when I should have been using:
.put("variable","TEST");
The document itself was using tags like
${variable}
The VariableReplace code will remove the ${} formatting whether a match is found or not. So if it is not finding a match, then the keys might not match the ones in the document for some reason, and this might not strictly be related to VariablePrepare. But this was a very helpful post for me since the VariablePrepare, VariableReplace solution is now working for my purposes.
Also, I am not sure that even VariablePrepare can handle the case where you change the font, highlighting or other formatting in the middle of your tag in the document. In such cases, it will not be able to merge the tag into a single run, and so tag recognition will likely fail.