FindReplaceOptions

FindReplaceOptions()

Initializes a new instance of the FindReplaceOptions class with default settings.

public FindReplaceOptions()

Examples

Shows how to recognize and use substitutions within replacement patterns.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.Write("Jason gave money to Paul.");

Regex regex = new Regex(@"([A-z]+) gave money to ([A-z]+)");

FindReplaceOptions options = new FindReplaceOptions();
options.UseSubstitutions = true;

// Using legacy mode does not support many advanced features, so we need to set it to 'false'.
options.LegacyMode = false;

doc.Range.Replace(regex, @"$2 took money from $1", options);

Assert.AreEqual(doc.GetText(), "Paul took money from Jason.\f");

See Also


FindReplaceOptions(FindReplaceDirection)

Initializes a new instance of the FindReplaceOptions class with the specified direction.

public FindReplaceOptions(FindReplaceDirection direction)
ParameterTypeDescription
directionFindReplaceDirectionThe direction of the find and replace operation.

See Also


FindReplaceOptions(IReplacingCallback)

Initializes a new instance of the FindReplaceOptions class with the specified replacing callback.

public FindReplaceOptions(IReplacingCallback replacingCallback)
ParameterTypeDescription
replacingCallbackIReplacingCallbackThe callback to use for replacing found text.

Examples

Shows how to track the order in which a text replacement operation traverses nodes.

public void Order(bool differentFirstPageHeaderFooter)
{
    Document doc = new Document(MyDir + "Header and footer types.docx");

    Section firstPageSection = doc.FirstSection;

    ReplaceLog logger = new ReplaceLog();
    FindReplaceOptions options = new FindReplaceOptions(logger);

    // Using a different header/footer for the first page will affect the search order.
    firstPageSection.PageSetup.DifferentFirstPageHeaderFooter = differentFirstPageHeaderFooter;
    doc.Range.Replace(new Regex("(header|footer)"), "", options);

    if (differentFirstPageHeaderFooter)
        Assert.AreEqual("First header\nFirst footer\nSecond header\nSecond footer\nThird header\nThird footer\n", 
            logger.Text.Replace("\r", ""));
    else
        Assert.AreEqual("Third header\nFirst header\nThird footer\nFirst footer\nSecond header\nSecond footer\n", 
            logger.Text.Replace("\r", ""));
}

/// <summary>
/// During a find-and-replace operation, records the contents of every node that has text that the operation 'finds',
/// in the state it is in before the replacement takes place.
/// This will display the order in which the text replacement operation traverses nodes.
/// </summary>
private class ReplaceLog : IReplacingCallback
{
    public ReplaceAction Replacing(ReplacingArgs args)
    {
        mTextBuilder.AppendLine(args.MatchNode.GetText());
        return ReplaceAction.Skip;
    }

    internal string Text => mTextBuilder.ToString();

    private readonly StringBuilder mTextBuilder = new StringBuilder();
}

See Also


FindReplaceOptions(FindReplaceDirectionIReplacingCallback)

Initializes a new instance of the FindReplaceOptions class with the specified direction and replacing callback.

public FindReplaceOptions(FindReplaceDirection direction, IReplacingCallback replacingCallback)
ParameterTypeDescription
directionFindReplaceDirectionThe direction of the find and replace operation.
replacingCallbackIReplacingCallbackThe callback to use for replacing found text.

See Also