We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Custom Range Replace to ignore comment runs

When using a custom Range.Replace IReplacingCallback and FindReplaceOptions is there a way to ignore runs inside of a comment?

I have code that looks for the first character in a document using the regex: ^. and adds a comment. If there is an existing comment at the start of the document, sometimes it finds the run inside the comment first. In my custom IReplacingCallback I can determine that the run is inside a comment and skip that result, but ^. Regex only matches the first character of a document, not the first character of each paragraph, so skipping doesn’t help me get to the first run outside of a comment.

On FindReplaceOptions would it be possible to have an IgnoreComments property similar to the existing IgnoreDeleted and IgnoreFields properties. Or better yet, it would be wonderful if the RegexOptions.Multiline was honored when performing a search. I have a separate thread that I just posted in with an additional example today where I have brought up this issue before. document.Range.Replace regex string anchors not working

@ResearchSquare

Please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

Here is a sample of the problem I am trying to solve

var doc = new Document();

var builder = new DocumentBuilder(doc);
builder.Writeln("My first paragraph");

var comment = new Comment(doc, "Jane", "JD", DateTime.Now);
comment.SetText("My wonderful comment");
builder.CurrentParagraph.PrependChild(comment);

doc.Range.Replace("My", "You're");

System.Console.WriteLine(doc.GetText());

You can see that in the output, the “My” in both the paragraph and the comment get replaced. But I would like for it to ignore the runs inside the comment. I know I can do this with a custom find replace method, but using that fix is blocked by the lack of multiline support in regex expressions.

@ResearchSquare

Please use the following code example to achieve your requirements. We suggest you please read the following article.

var doc = new Document();

var builder = new DocumentBuilder(doc);
builder.Writeln("My first paragraph");

var comment = new Comment(doc, "Jane", "JD", DateTime.Now);
comment.SetText("My wonderful comment");
builder.CurrentParagraph.PrependChild(comment);

FindReplaceOptions findReplaceOptions = new FindReplaceOptions();
findReplaceOptions.ReplacingCallback = new TextFindAndReplace();
doc.Range.Replace("My", "You're", findReplaceOptions);

System.Console.WriteLine(doc.GetText());
private class TextFindAndReplace : IReplacingCallback
{
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs args)
    {
        if (args.MatchNode.GetAncestor(NodeType.Comment) != null)
            return ReplaceAction.Skip;
        else
            return ReplaceAction.Replace;
    }
}

Thank you for the quick response. I am actually already doing that and using a custom find and replace method. The issue then becomes the lack of regex multiline support in Aspose which is the other ticket that I have linked to. We can close out this ticket but it would be helpful if you can investigate document.Range.Replace regex string anchors not working which would allow me to use the IReplacingCallback.

@ResearchSquare

Thanks for your feedback. We have already answered your query [here](https://forum.aspose.com/t/document-range-replace-regex-string-anchors-not-working/210504/8) in this post. Please follow that thread for further proceedings.