FindReplaceOptions FindWholeWordsOnly bug



I am trying to use replace functionality on my Aspose Word doc in Java. Here is my code:

public com.aspose.words.Document replaceFieldValues(ContractCDO contract, InputStream is) throws Exception {
com.aspose.words.Document doc = new com.aspose.words.Document(is);
HashMap<String, String> fields = prepareFieldsMap(contract);
//go through fields and replace
fields.forEach((key, value) -> {
try {
FindReplaceOptions findReplaceOptions = new FindReplaceOptions();
findReplaceOptions.FindWholeWordsOnly = true;

            doc.getRange().replace(Pattern.compile("\\$"+key), value, findReplaceOptions);
        } catch (Exception e) {
    return doc;

This works fine in most cases, but it doesn’t replace only whole words. For instance if I have $IMPORT_TEXT and $IMPORT_TEXTAREA in my document, and I try to replace them with the text ‘One’ and ‘Two’ respectively, the result is surprising. Instead of having values One and Two in my document, I will have One and then OneAREA.
I am using version 17.3.0 of the Aspose Words for Java.



Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please create a simple Java application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.


Just to confirm before I make a sample application: Your functionality for replacing pattern in the document when using FindWholeWordsOnly generally works? It doesn’t find words that only begin with the selected pattern?
Because it seems to me that this is not working at all and that there is absolutely no point in spending my valuable time in creating an example when this is not working at all. Especially since I send you my code and only get your generic first-line-of-support response as usually. Please confirm if it works in any other situation.



Thanks for your inquiry. Yes, FindReplaceOptions.FindWholeWordsOnly property works as expected. We have tested the scenario using simple test document and have not found the shared issue. You are using older Aspose.Words. We suggest you please upgrade to the latest version of Aspose.Words for Java 17.10.

If you still face the problem, please share the requested detail. We will investigate the issue and provide you more information on this.


Hi, we updated our Aspose version, and we are still experiencing the same issue. I am sending you our input file, output file, and sample code.

aspose find (22.0 KB)



Thanks for your inquiry. Unfortunately, we are unable to download the shared attachment. Could you please attach it again? Thanks for your cooperation.

#7 (22.0 KB)

Please find attachment above



Thanks for sharing the detail. We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-16217. You will be notified via this forum thread once this issue is resolved. We apologize for your inconvenience.

Could you please share the Java version that you are using at your end? Thanks for your cooperation.


Thanks for your help. We are using Java 1.8_112.

Best regards



Thanks for sharing the Java version. You will be notified via this forum thread once this issue is resolved.


Hi Tahir,

Any update on this?




Thanks for your inquiry. We try our best to deal with every customer request in a timely fashion, we unfortunately cannot guarantee a delivery date to every customer issue. Our developers work on issues on a first come, first served basis. We feel this is the fairest and most appropriate way to satisfy the needs of the majority of our customers.

Currently, your issue is pending for analysis and is in the queue. Once our product team completes the analysis of your issue, we will then be able to provide you an estimate.

Thanks for your patience and understanding.


Hi Tahir,

Any update on this?



Thanks for your inquiry. We have asked for the ETA of this issue from our product team. As soon as any information is shared by them, we will be more than happy to share that with you.

Thanks for your patience.


Hi Tahir,

Any update on this?



Thanks for your inquiry. Unfortunately, there is no update available on this issue at the moment. We have asked again for the ETA of this issue from our product team. We will inform via this forum thread you as soon as there is any update available. Thanks for your patience.



Thanks for your patience. As a workaround of this issue, please use \b modifier in regular expression pattern for example @"\bIMPORT_TEXTAREA\b". Or you can try Replace method accepted string as pattern. For example:

doc.getRange().replace(“IMPORT_TEXT”, “testing”, findReplaceOptions);

Unfortunately, you cannot use $ sign as part of pattern in both cases since \b modifier or FindWholeWordsOnly works for alphanumeric words only.



Thanks for the workaround. Unfortunately, we want to replace only text that contains $ sign.



Thanks for your inquiry. We will inform you via this forum thread once this issue is resolved. We apologize for your inconvenience.

You reported this issue in free support forum and it will be treated with normal priority. To speed up the progress of issue’s resolution, we suggest you please check our paid support policies from following link.
Paid Support Policies



Thanks for your patience. Please use Range.Replace method as shown below. Hope this helps you.

Document doc = new Document(MyDir + "replace.docx");

FindReplaceOptions findReplaceOptions = new FindReplaceOptions();
findReplaceOptions.FindWholeWordsOnly = true;
doc.Range.Replace(new Regex(@"\$IMPORT_TEXT\b"), "testing", findReplaceOptions);
doc.Range.Replace(new Regex(@"\$IMPORT_TEXTAREA\b"), "testing", findReplaceOptions);

doc.Save(MyDir + "18.2.docx");