RangeReplace Method (String, String, FindReplaceOptions)

Replaces all occurrences of a character pattern specified by a regular expression with another string.

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.2
Syntax
public int Replace(
	string pattern,
	string replacement,
	FindReplaceOptions options
)

Parameters

pattern
Type: SystemString
A string to be replaced.
replacement
Type: SystemString
A string to replace all occurrences of pattern.
options
Type: Aspose.Words.ReplacingFindReplaceOptions
FindReplaceOptions object to specify additional options.

Return Value

Type: Int32
The number of replacements made.
Remarks

Replaces the whole match captured by the regular expression.

Method is able to process breaks in both pattern and replacement strings.

You should use special meta-characters if you need to work with breaks:
  • &p - paragraph break
  • &b - section break
  • &m - page break
  • &l - manual line break
To leave any meta-character intact a parameter PreserveMetaCharacters should be set to true.
Examples
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Numbers 1, 2, 3");

// Inserts paragraph break after Numbers.
doc.Range.Replace("Numbers", "Numbers&p", new FindReplaceOptions());
Examples
Simple find and replace operation.
// Open the document
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.Writeln("Hello _CustomerName_,");

// Check the document contains what we are about to test
Console.WriteLine(doc.FirstSection.Body.Paragraphs[0].GetText());

FindReplaceOptions options = new FindReplaceOptions();
options.MatchCase = false;
options.FindWholeWordsOnly = false;

// Replace the text in the document
doc.Range.Replace("_CustomerName_", "James Bond", options);

// Save the modified document
doc.Save(ArtifactsDir + "Range.ReplaceSimple.docx");
Examples
Shows how to replace text in the document footer.
// Open the template document, containing obsolete copyright information in the footer
Document doc = new Document(MyDir + "Footer.docx");

HeaderFooterCollection headersFooters = doc.FirstSection.HeadersFooters;
HeaderFooter footer = headersFooters[HeaderFooterType.FooterPrimary];

FindReplaceOptions options = new FindReplaceOptions
{
    MatchCase = false,
    FindWholeWordsOnly = false
};

int currentYear = System.DateTime.Now.Year;
footer.Range.Replace("(C) 2006 Aspose Pty Ltd.", $"Copyright (C) {currentYear} by Aspose Pty Ltd.", options);

doc.Save(ArtifactsDir + "HeaderFooter.ReplaceText.doc");
Examples
Shows how to replace all instances of String of text in a table and cell.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Create a table and give it conditional styling on border colors based on the row being the first or last
Table table = builder.StartTable();
builder.InsertCell();
builder.Write("Carrots");
builder.InsertCell();
builder.Write("30");
builder.EndRow();
builder.InsertCell();
builder.Write("Potatoes");
builder.InsertCell();
builder.Write("50");
builder.EndTable();

FindReplaceOptions options = new FindReplaceOptions();
options.MatchCase = true;
options.FindWholeWordsOnly = true;

// Replace any instances of our String in the entire table
table.Range.Replace("Carrots", "Eggs", options);
// Replace any instances of our String in the last cell of the table only
table.LastRow.LastCell.Range.Replace("50", "20", options);

doc.Save(ArtifactsDir + "Table.ReplaceCellText.doc");
See Also