"The match includes one or more special or break characters and cannot be replaced."

Hi,

The following code throws me error at Range.Replace…

public void FindString()
{
    try {
        Document doc = new Document(@"C:\Program Files\Aspose\Aspose.Word\Demos\Documents\AUProposal\_Seeded\_Mapped\_1.doc");
        i = 0;
        doc.Range.Replace(new Regex(@"({CLAUSE\#:BEGIN:256:AU\_Proposal:\#CLAUSE})[\S\s\n\r\f\v\e\.]\*?({CLAUSE\#:END:256:AU\_Proposal:\#CLAUSE})"), new ReplaceEvaluator(MyReplaceEvaluator), true);
        string str = doc.Range.Text;
        doc.Save(@"C:\Program Files\Aspose\Aspose.Word\Demos\Documents\AUProposal\_Seeded\_Mapped\_1\_out.doc");
    }
}

private ReplaceAction MyReplaceEvaluator(object sender, ReplaceEvaluatorArgs e)
{
    e.Replacement = e.Match.Value + "James Bond" + i.ToString();
    i++;
    return ReplaceAction.Replace;
}

private int i;

The regular expression is correct. We have verified it using other tools.

Basically, we want to replace a large fragment of data with the replacement string. But we are getting the error "The match includes one or more special or break characters and cannot be replaced."

Please suggest

Thanks

mano

Hi,

Please find attached the target word document for your reference.

Thanks

mano

https://reference.aspose.com/words/net/aspose.words.range/replace/methods/2

The API documentation says:

An exception is thrown if a captured or replacement string contain one or more special characters: paragraph break, cell break, section break, field start, field separator, field end, inline picture, drawing object, footnote.

Aspose.Word find and replace is not yet powerful enough to capture a whole table with cells paragraphs etc and replace it with a simple string. You can only find and replace text that does not span more than one paragraph.

If you want to be able to remove or replace a region like in your document, you have several options.

  1. Enclose the text in a bookmark and then use:
//To delete bookmark contents and/or insert simple text
Bookmarks["MyBookmark"].Text = "replacement text";

//If you need to insert more complex content or formatting:
DocumentBuilder.MoveToBookmark("MyBookmark");
DocumentBuilder.InsertXXX and other methods to insert content and formatting.
  1. Enclose the text in a section with continious section breaks (that do not start a new page). Then you will be able to doc.Sections[1].Remove() to delete the section from the document based on some logic.

…so 7 years ago, the search/replace ‘wasn’t powerful enough’ to deal with this. Is it now, 7 years on?

Hi

Thanks for your request and sorry for delayed response. Unfortunately, Aspose.Words still cannot capture special characters when you use Replace functionality. I linked your request the the appropriate issue. We will let you know once this feature is improved.

Best regards,

Hi!
We got the same Exception with the following text and pattern:
Text:
blubb…

Pattern:<<\[\[(.|\s)*?\]\]>>)|(<<(.|\s)*?>>

So, we need to find and replace a pattern over 2 or more paragraphes. Please don’t tell me, this is still not possible…

Best regards,
Frank

PS:
A solution with bookmarks is not an option for us!

Hi Frank,

Thanks for your inquiry.

Unfortunately, WORDSNET-1252 is not resolved yet. I have verified the status of this issue from our issue tracking system and regret to share with you that this issue has been postponed till a later date and there is no reliable ETA available. We will be sure to inform you of any developments and let you know once it is resolved. We apologize for any inconvenience.

However, in the meantime while you’re waiting for a fix, please try run the following code snippet as a workaround.

Document doc = new Document(@"C:\Temp\in.docx");
Node startPara = null;
Node endPara = null;
NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
foreach (Paragraph paragraph in paragraphs)
{
    if (paragraph.Range.Text.StartsWith("<<[["))
        startPara = paragraph;
    if (paragraph.Range.Text.StartsWith("]]>>"))
        endPara = paragraph;
}
if (startPara != null && endPara != null)
{
    Paragraph replacementPara = new Paragraph(doc);
    replacementPara.Runs.Add(new Run(doc, "your replacement string goes here"));
    startPara.ParentNode.InsertBefore(replacementPara, startPara);
    Node currentNode = startPara;
    bool isRemoving = true;
    while (currentNode != null && isRemoving)
    {
        if (currentNode == endPara)
            isRemoving = false;
        Node nextNode = currentNode.NextSibling;
        currentNode.Remove();
        currentNode = nextNode;
    }
}
doc.Save(@"C:\Temp\out.docx");```
I hope, this helps.

Best regards,

Hi Awais,

this is a bad news.

We constructed a replacement-engine, based on regex… Your code-snippet may help in this specific case, but we have to build a complete new replacement-engine for all of our use-cases. This is a lot of work…
So, we’ll have to wait for a solution.

Is there any possibilty to increase the priority of this issue?

Thanks for your feedback!

Best regards,
Frank

Hi Frank,

Thanks for your inquiry.

Well, keeping in view the complexity and the amount of work involved in implementing the fix, I am afraid, we can not promise you an immediate resolution. However, we understand that this issue is important to you and it is unfortunate that the issue has not yet been fixed. We apologise for your inconvenience.

Regarding raising the priority, please have a look at this page - http://www.aspose.com/corporate/services/priority-support-policies.aspx - purchasing Priority Support will allow you to post your issues in our Priority Support forum and raise the priority of these issues directly with our development teams, if possible, we will then aim to get a resolution to your issue as soon as we can. Many Priority Support customers find that this leads to their issue being fixed in the next release of the software.

If you would like to take advantage of Priority Support then please request a quote in our purchase forum - https://forum.aspose.com/c/purchase/6

Best regards,

Hello,

I was able to get this to work with a couple of things:

  1. My replace handler first looks at e.MatchNode.Range.Text and makes sure it contains the start text. If it doesn’t, it moves back through the tree to find the first node that does.

  2. Use the SplitRun function from the samples to split the starting run so you have a run with your start text at the very beginning.

  3. Create an ArrayList of all the runs in between your starting run and the run where you find the end text. You’ll have to traverse up and down the tree structure, this will pick up your paragraphs and other nodes.

  4. Loop through all your runs and remove them, if the parent node is a paragraph and it only has one child, I just removed the entire paragraph.

  5. Return ReplaceAction.Skip

  6. When you use Range.Replace, specify False for the isForward parameter. This will make the search go from the bottom of your document up. You’ll get weird results if you start at the top and remove runs.

If you need to know how many were replaced, keep a counter in your class and add a property to get the count after the replace has finished.

Hi Ed,

Thanks for the additional information. It is great you were able to find what you were looking for. Rest assured, we will inform you via this thread as soon as WORDSNET-1252 is resolved. We apologize for any inconvenience.

Best regards,

The issues you have found earlier (filed as WORDSNET-1252) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(39)