We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Escaping meta character in Replace isn't working

There appears to be a bug with escaping meta characters in the replace string when using Range.Replace(). For example, if I want to literally replace with “&p” in the document (an ampersand followed by the letter p), according to the documentation, it seems like &&p should work. However, this doesn’t work – I still get a paragraph character inserted in the document. The same applies to the other metacharacters (I have tried all of them).

Also, multiple ampersands don’t see to work correctly. & inserts a single ampersand, && also inserts a single ampersand, &&&& still inserts a single ampersand – I need 5 of them &&&&& to insert 2 ampersand characters in the document.

@ssmolkin1,

Please compress the following resources into ZIP format and attach the .zip file here for testing:

  • Your simplified source Word document
  • Aspose.Words for .NET 21.5 generated output DOCX file showing the undesired behavior
  • Source code you used to generate above DOCX file
  • Your expected DOCX file showing the desired output. You can create this expected document manually by using MS Word.

As soon as you get these pieces of information ready, we will start further investigation into your issue and provide you more information.

@ssmolkin1,

After an initial test with the latest (21.5) version of Aspose.Words for .NET, we were unable to reproduce this issue on our side. The Range.Replace() method seems to work fine. For example, the following line of code replaces the word “same” in the document with “&p” as expected.

doc.Range.Replace("same", "&&p", new FindReplaceOptions());

Also, when using the “&&&” string to replace a character string in the document, the Range.Replace() method replaces it with 2 ampersand characters as expected.
Please, make sure that you use the latest version (21.5) version of Aspose.Words for .NET.

I am still seeing this error even after updating to 21.5. Please see attached input, output and expected files. Please see below arrays of find and replace text. Please try to replace each of the strings in the find array with the matching text (same index) from the replace array.

string[] find = new string[] { "&&&&", "&&", "ampersand", "ampamp", "para", "section", "page", "line" };
string[] replace = new string[] { "double-amp", "and", "&&", "&&&&", "&&p", "&&b", "&&m", "&&l" };

actually.docx (9.3 KB)
expected.docx (12.0 KB)
input.docx (11.9 KB)

@ssmolkin1,

We tried to reproduce the issue, using your input file and related code. Unfortunately, we were unable to get the incorrect output - our generated file output.docx (9.3 KB) contains exactly the same content as the “expected.docx” file.
Сould you please create a simple console app and attach it to your post, so we can reproduce the problem on our side?