I am adding comment to Date

Hi Team,
Below code I used to add the comment for date. this code is not working for below mentioned input doc file.
Test_Input.docx (14.0 KB)
Test_expected_output.docx (19.0 KB)

public static void DateFormat(Document doc)
{
    doc.StopTrackRevisions();
    FindReplaceOptions options = new FindReplaceOptions();
    options.ReplacingCallback = new ReplaceEvaluatorWrapWithComment();

    options.UseSubstitutions = true;
    options.IgnoreDeleted = true;
    options.MatchCase = false;
    GeneralStandardUtility generalStandardUtility = new GeneralStandardUtility();
    var getMonths = generalStandardUtility.GetMonths();

    //var pattern = "";

    //01-APR-2023, 01-apr-2023 ,01-APR-22, 01-apr-22,

    doc.Range.Replace(new Regex(@"\b\d{1,2}-(?!May)(?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)-(\d{4}|\d{2})\b"), "", options);

    //01-JAN-2022
    doc.Range.Replace(new Regex(@"\b\d{1,2}-(?!May)(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)-(\d{4}|\d{2})\b"), "", options);

    //01-April-2023, 01 - APRIL - 2023, 01 - april - 2023,01-April-22, 01 - APRIL - 22, 01 - april - 22

    doc.Range.Replace(new Regex(@"\d{1,2}-(?i)(?!May)(January|February|March|April|May|June|July|August|September|October|November|December)-(\d{4}|\d{2})"), "", options);

    ////01April2023, 01APRIL2023 ,01april2023, 01April23, 01APRIL23 ,01april23
    //doc.Range.Replace(new Regex(@"\d{1,2}(?i)(?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)(\d{4}|\d{2})"), "", options);

    //Extra code May
    doc.Range.Replace(new Regex(@"\d{1,2}(?!May)(?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)(\d{4}|\d{2})"), "", options);
    doc.Range.Replace(new Regex(@"\d{1,2}(?!May)(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)(\d{4}|\d{2})"), "", options);




    ////01April2023 ,01APRIL2023 ,01april2023 , 01April23 ,01APRIL23 ,01april23
    //doc.Range.Replace(new Regex(@"\d{1,2}(?i)(January|February|March|April|May|June|July|August|September|October|November|December)(\d{4}|\d{2})"), "", options);

    //extra code for May
    doc.Range.Replace(new Regex(@"\d{1,2}(?!May)(?i)(January|February|March|April|May|June|July|August|September|October|November|December)(\d{4}|\d{2})"), "", options);



    //01-APR-2023, 01-apr-2023 ,01-APR-22, 01-apr-22,

    doc.Range.Replace(new Regex(@"\d{1,2}/(?i)(?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)/(\d{4}|\d{2})"), "", options);

    //01-April-2023, 01 - APRIL - 2023, 01 - april - 2023,01-April-22, 01 - APRIL - 22, 01 - april - 22

    doc.Range.Replace(new Regex(@"\d{1,2}/(?i)(January|February|March|April|May|June|July|August|September|October|November|December)/(\d{4}|\d{2})"), "", options);



    //1/4/22, 01 / 04 / 2022 ,2 / 3 / 2022 ,2 / 3 / 22, 1 / 2 / 22 ,1 / 4 / 22, 05 / 01 / 2022 ,5 / 01 / 2022, 05 / 1 / 2022, 05 / 01 / 22
    //5 / 1 / 2022, 5 / 1 / 22 ,05 / may / 2022, 4APR22

    doc.Range.Replace(new Regex(@"\b(0?[1-9]|[12]\d|3[01])\/(0?[1-9]|1[0-2])\/(\d{2}|\d{4})\b"), "", options);

    //01-01-2023 ,01-01-22
    doc.Range.Replace(new Regex(@"\b(0?[1-9]|[12]\d|3[01])-(0?[1-9]|1[0-2])-(\d{2}|\d{4})\b"), "", options);

}

internal class ReplaceEvaluatorWrapWithComment : IReplacingCallback
{
    /// <summary>
    /// This method is called by the Aspose.Words find and replace engine for each match.
    /// </summary>
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    {
        Document doc = (Document)e.MatchNode.Document;

        // This is a Run node that contains either the beginning or the complete match.
        Node currentNode = e.MatchNode;

        // The first (and may be the only) run can contain text before the match, 
        // in this case it is necessary to split the run.
        if (e.MatchOffset > 0)
            currentNode = SplitRun((Run)currentNode, e.MatchOffset);

        // This array is used to store all nodes of the match for further deleting.
        List<Run> runs = new List<Run>();

        // Find all runs that contain parts of the match string.
        int remainingLength = e.Match.Value.Length;
        while (
            remainingLength > 0 &&
            currentNode != null &&
            currentNode.GetText().Length <= remainingLength)
        {
            runs.Add((Run)currentNode);
            remainingLength -= currentNode.GetText().Length;

            // Select the next Run node.
            // Have to loop because there could be other nodes such as BookmarkStart etc.
            do
            {
                currentNode = currentNode.NextSibling;
            } while (currentNode != null && currentNode.NodeType != NodeType.Run);
        }

        // Split the last run that contains the match if there is any text left.
        if (currentNode != null && remainingLength > 0)
        {
            SplitRun((Run)currentNode, remainingLength);
            runs.Add((Run)currentNode);
        }

        // Create comment.
        Comment comment = new Comment(doc, "Alexey", "AN", DateTime.Now);
        // Add some content to the comment.
        comment.EnsureMinimum();
        comment.FirstParagraph.AppendChild(new Run(doc, "The date format must be dd-Mmm-yyyy and DDMmmYYYY"));

        // Create comment range start and end.
        CommentRangeStart start = new CommentRangeStart(doc, comment.Id);
        CommentRangeEnd end = new CommentRangeEnd(doc, comment.Id);

        // Wrap matched content with comment.
        runs[0].ParentNode.InsertBefore(start, runs[0]);
        runs[0].ParentNode.InsertBefore(comment, runs[0]);
        runs[runs.Count - 1].ParentNode.InsertAfter(end, runs[runs.Count - 1]);

        // Signal to the replace engine to do nothing because we have already done all what we wanted.
        return ReplaceAction.Skip;
    }

    private static Run SplitRun(Run run, int position)
    {
        Run afterRun = (Run)run.Clone(true);
        run.ParentNode.InsertAfter(afterRun, run);
        afterRun.Text = run.Text.Substring(position);
        run.Text = run.Text.Substring((0), (0) + (position));
        return afterRun;
    }
}

@Princeshivananjappa based on your code you should me de the following changes:

  1. In your main function you should add the following replacement case:
...
//Extra code May
doc.Range.Replace(new Regex(@"\d{1,2}(?!May)(?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)(\d{4}|\d{2})"), "", options);

// Line to add
doc.Range.Replace(new Regex(@"\d{1,2}(?!May)(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)(\d{4}|\d{2})"), "", options);

doc.Range.Replace(new Regex(@"\d{1,2}(?!May)(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)(\d{4}|\d{2})"), "", options);
...
  1. In the Replacing method of the class ReplaceEvaluatorWrapWithComment, change the way in where you insert the comment text:
...
// Create comment.
Comment comment = new Comment(doc, "Alexey", "AN", DateTime.Now);
// Add some content to the comment.
comment.EnsureMinimum();

// Separate the run creation to make easier to set the font color
var commentRun = new Run(doc, "The date format must be dd-Mmm-yyyy");
commentRun.Font.Color = Color.Red;
comment.FirstParagraph.AppendChild(commentRun);
...