FindReplace and Regular Expressions

Hello,

I am looking for some guidance. I am trying to find specific tags within a Word document so that I can act/format the document accordingly. For example, there can be a tag [BOLD] This is a test [/BOLD] where I need to find the start and end tags of [BOLD], remove the tags but then format the text in between the tags.

I am using a Regular Expression so that I can capture the begin and end tag versus just trying to find the string value of the tag (since I am formatting).

However when I use this, the ReplacingCallback gets called once but gives me all of the text in Match.Value and shows only one run.

Is there a better way to find text based on a start and end tag and get each instance to work with?

Here is my small code snippet if the FindReplaceOptions

FindReplaceOptions options = new FindReplaceOptions();
options.ReplacingCallback = new ReplaceAndFormatText(options);
wordDoc.Range.Replace(new Regex("\\[BOLD\\].*\\[\\/BOLD\\]"), String.Empty, options);

Here is my ReplacingCallback implementation

private class ReplaceAndFormatText : IReplacingCallback
{
    private FindReplaceOptions _options = null;
    internal ReplaceAndFormatText(FindReplaceOptions options)
    {
        _options = options;
    }

    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs args)
    {
        Console.WriteLine(args.Match.Value);
        return ReplaceAction.Skip;
    }
}

I can attach a sample Word document but it can be simple as the following

[BOLD]1. [UNDERLINE]Proceeding No.AAAA-0000000000[/UNDERLINE][/BOLD]
title stuff
a. Filing Date and Party:
Description:
Analyst(s):
[BOLD]2. [UNDERLINE]Proceeding No.BBBBB-1111111111[/UNDERLINE][/BOLD]
test test
a. Filing Date and Party:
Description:
Analyst(s):

@icordova In your case you can simply use FindReplaceOptions.ApplyFont and FindReplaceOptions.UseSubstitutions. For example see the following code:

Document doc = new Document(@"C:\Temp\in.docx");

// Mark with bold
FindReplaceOptions optBold = new FindReplaceOptions();
optBold.UseSubstitutions = true;
optBold.ApplyFont.Bold = true;
doc.Range.Replace(new Regex("\\[BOLD\\](.*?)\\[\\/BOLD\\]"), "$1", optBold);
// Mark with underline
FindReplaceOptions optUnderline = new FindReplaceOptions();
optUnderline.UseSubstitutions = true;
optUnderline.ApplyFont.Underline = Underline.Single;
doc.Range.Replace(new Regex("\\[UNDERLINE\\](.*?)\\[\\/UNDERLINE\\]"), "$1", optUnderline);

doc.Save(@"C:\Temp\out.docx");

Here is the input and output produced by the above code:
in.docx (12.5 KB)
out.docx (9.9 KB)

Thank you @alexey.noskov! That worked! I appreciate the assistance.

1 Like