RangeReplace Method (Regex, 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(
	Regex pattern,
	string replacement,
	FindReplaceOptions options
)

Parameters

pattern
Type: System.Text.RegularExpressionsRegex
A regular expression pattern used to find matches.
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("a1, b2, c3");

// Replaces each number with paragraph break.
doc.Range.Replace(new Regex(@"\d+"), "&p", new FindReplaceOptions());
Examples
Shows how to replace all occurrences of words "sad" or "mad" to "bad".
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("sad mad bad");

Assert.AreEqual("sad mad bad", doc.GetText().Trim());

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

doc.Range.Replace(new Regex("[s|m]ad"), "bad", options);

Assert.AreEqual("bad bad bad", doc.GetText().Trim());
Examples
Shows how to insert content of one document into another during a customized find and replace operation.
public void InsertDocumentAtReplace()
{
    Document mainDoc = new Document(MyDir + "Document insertion destination.docx");

    FindReplaceOptions options = new FindReplaceOptions();
    options.Direction = FindReplaceDirection.Backward;
    options.ReplacingCallback = new InsertDocumentAtReplaceHandler();

    mainDoc.Range.Replace(new Regex("\\[MY_DOCUMENT\\]"), "", options);
    mainDoc.Save(ArtifactsDir + "InsertDocument.InsertDocumentAtReplace.doc");
}

private class InsertDocumentAtReplaceHandler : IReplacingCallback
{
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs args)
    {
        Document subDoc = new Document(MyDir + "Document.docx");

        // Insert a document after the paragraph, containing the match text
        Paragraph para = (Paragraph) args.MatchNode.ParentNode;
        InsertDocument(para, subDoc);

        // Remove the paragraph with the match text
        para.Remove();

        return ReplaceAction.Skip;
    }
}
Examples
Replaces text specified with regular expression with HTML.
public void ReplaceWithInsertHtml()
{
    // Open the document
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    builder.Writeln("Hello <CustomerName>,");

    FindReplaceOptions options = new FindReplaceOptions();
    options.ReplacingCallback = new ReplaceWithHtmlEvaluator(options);

    doc.Range.Replace(new Regex(@" <CustomerName>,"), string.Empty, options);

    // Save the modified document
    doc.Save(ArtifactsDir + "Range.ReplaceWithInsertHtml.doc");

}

private class ReplaceWithHtmlEvaluator : IReplacingCallback
{
    internal ReplaceWithHtmlEvaluator(FindReplaceOptions options)
    {
        mOptions = options;
    }

    /// <summary>
    /// NOTE: This is a simplistic method that will only work well when the match
    /// starts at the beginning of a run.
    /// </summary>
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs args)
    {
        DocumentBuilder builder = new DocumentBuilder((Document) args.MatchNode.Document);
        builder.MoveTo(args.MatchNode);

        // Replace '<CustomerName>' text with a red bold name
        builder.InsertHtml("<b><font color='red'>James Bond, </font></b>");
        args.Replacement = "";

        return ReplaceAction.Replace;
    }

    private readonly FindReplaceOptions mOptions;
}
See Also