Search code examples
asposeaspose.words

Aspose.Words - Find and replace functionality - find text with new line characters


I am trying to search and replace the below text in a word document using Aspose.Words (Version 22.4). But, the Aspose replace function is not able to find and replace the text in the document.

[SERVICE WAITING PERIOD:

[[30 days] of Active continuous service.]]

I am using the below code, where

rule.MergeField = "[SERVICE WAITING PERIOD:\r\n[[30 days] of Active continuous service.]]";
mergedDocument.Range.Replace(rule.MergeField.Replace("\n", "&l").Replace("\r", "&p"),
"abc", new Aspose.Words.Replacing.FindReplaceOptions()
{ MatchCase = false, FindWholeWordsOnly = false });

I have also tried replacing the \n, \r in text with the Aspose Control Characters without any luck. Has anyone tried something like this or does aspose support such search text?

Thank you for your help.


06/09 Updates

Replacing \r\n with &p as suggested by Alexey worked. thank you so much. Need help on the metacharacters for aspose for below text.

\r

Example text - Age at Date of Loss\rPercent of Original Benefit Amount\r[Age 70-74]\r[65%]\r[Age 75-79]\r[45%]\r[Age 80-84]\r[30%]\r[Age 85 or over]\r[15%]\r

Word equivalent -

enter image description here

\t (Tab)

Example text - COVERED LOSS OF USE OF\tPERCENTAGE OF COVERAGE AMOUNT\r\n\tFour Limbs\t[100%]\r\n\tThree Limbs\t[75%]\r\n\tTwo Limbs\t[66.67%]\r\n\tOne Limb\t[50%]\r\n

Word equivalent - enter image description here

UPDATES 06/15

I tried to search and replace the text with \t but it does not work with below code.

 rule.MergeField = "Class II:\t\tWhile participating in game, please make sure to wear your helmets.";

 mergedDocument.Range.Replace(rule.MergeField.Replace("\r\n", "&p").Replace("\t", ControlChar.Tab),
 finalValue.Replace("\r\n", "&p").Replace("\t", "&l"), new Aspose.Words.Replacing.FindReplaceOptions()
{ MatchCase = false, FindWholeWordsOnly = false });

Solution

  • You should replace a sequence \r\n with &p metacharacter to match paragraph break in the Document. For example see the following code:

    Document doc = new Document(@"C:\Temp\in.docx");
    
    string match = "[SERVICE WAITING PERIOD:\r\n[[30 days] of Active continuous service.]]";
    
    // Replace \r\n sequence (which usualy represents paragraph break in text documents) with &p metacharacter
    // to match paragraph break in MS Word document.
    match = match.Replace("\r\n", "&p");
    
    doc.Range.Replace(match, "abc", new FindReplaceOptions() { MatchCase = false, FindWholeWordsOnly = false });
    
    doc.Save(@"C:\Temp\out.docx");
    

    &l metacharacter represent a soft line break, which is \v, you can insert such break in MS Word using Shift+Enter

    UPDATE:

    You can use \t character in your match string and replacement.

    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);
    // Insert some content with tab character.
    builder.Write("This\tis\ttext\twith\ttab\tcharacters.");
    // Save an intermadiate result.
    doc.Save(@"C:\Temp\interm.docx");
    
    // Now replace all tabs with sequence of whitespaces.
    doc.Range.Replace("\t", "    ");
    // Save the result.
    doc.Save(@"C:\Temp\out.docx");
    

    The other character you have highlighted is cell break. You cannot replace it. Table cell in MS Word always contain a paragraph and the highlighted character is the end of the last paragraph in the cell. It cannot be removed.