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

Free Support Forum - aspose.com

FindReplaceOptions FindWholeWordsOnly bug - does not work

I’m using the latest version of Aspose for .NET (4.8). It appears that the FindReplaceOptions methods does NOT honor the FindWholeWordsOnly flag. To reproduce this, you can use Standard examples provided by Aspose. Use the FindAndHighlight TestCase, then modify:

        FindReplaceOptions options = new FindReplaceOptions
        {
            ReplacingCallback = new ReplaceEvaluatorFindAndHighlight(), Direction = FindReplaceDirection.Backward
        };

to

        FindReplaceOptions options = new FindReplaceOptions
        {
            ReplacingCallback = new ReplaceEvaluatorFindAndHighlight(), Direction = FindReplaceDirection.Backward,
            FindWholeWordsOnly = true
        };

Change the Regex search string from “your document” to “test” and then add the following paragraph to the end of the test document (“Find and highlight.docx”):

test, test’s, tested, testing, tests, tests’, testbed, testbed typing-test stuff afterwards

If you run the test, that last line will have the word “test” highlighted in every one of those words, even though it should ONLY be the first 2 and the last one (do the same in Word to see what it does).

Thanks,
Andy.

@bsant Thank you for reporting this problem to us. I have logged it as WORDSNET-23258.
It occurs because IReplacingCallback is called before processing the FindWholeWordsOnly option. This makes an expression the option is ignored. If you use code like the following to highlight matched text, the result will be correct:

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

FindReplaceOptions options = new FindReplaceOptions();
options.ApplyFont.HighlightColor = Color.Yellow;
options.FindWholeWordsOnly = true;

// Replace highlighted text.
doc.Range.Replace("test", "test", options);

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

We will keep you updated and let you know once the issue is resolved.

Hi Alexey,

Thanks for the update, Yep, we found that work-around for the word highlighting, although it has other “issues” like the casing of letters and the removing of any other formatting, so it doesn’t really work either! We do have other things we need to do with the replace, so we do need the defect fixed as well.

Thanks,
Andy

@bsant Thank you for additional information information. We will keep you informed and let you know once the issue is resolved.

The issues you have found earlier (filed as WORDSNET-23258) have been fixed in this Aspose.Words for .NET 22.2 update also available on NuGet.