Search code examples
asposeaspose.words

Aspose.Words - Range.Parse(regex, value, FindReplaceOptions) method can't find text specified with regex in paragraph but in table cell it does


I wrote simple word document with one paragraph and one table (with one cell) under that paragraph. I'm using Aspose 16.7 and Aspose 22.9 (on both versions I have same problem).

When I open that word document using aspose it will look like this:

\r\r\r<<AC:doc_title:value>>\r<<AC:doc_title:value>>\a\a\rTest\r\r\r

Replace method won't work when it tries to find and replace <<AC:doc_title:value>> which is in paragraph but when I put same tag in table cell, replace method will find that tag and replace it with given text. This is my replace method call:

node.Range.Replace(new Regex("<<AC:doc_title:value>>"), "Replaced text", new FindReplaceOptions(FindReplaceDirection.Forward));

I tried to call Parse method with different FindReplaceOptions but that didn't give any results. I also tried Replace method with only two parameters, node.Range.Replace(new Regex("<<AC:doc_title:value>>"), value) and when using this method, I didn't have any problems, it works fine (but problem is that method is Obsolete now).

Thank you for your help.


Solution

  • Range.Replace(Regex, string) overload is no marked as obsolete. So you can use it: https://reference.aspose.com/words/net/aspose.words/range/replace/#replace_2

    This overload internally calls the overload with FindReplaeOptions, so both should work the same:

    public int Replace(Regex pattern, string replacement)
    {
        return Replace(pattern, replacement, new FindReplaceOptions());
    }
    

    If you still has problems, please post the question in Aspose.Words support forum and attach your input document there for testing.