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

Free Support Forum - aspose.com

Troubleshooting find and replace with regular expressions

Hi Aspose,


We are in the final phase of evaluating your product. Apart from many (great) working features, we are having a problem searching with regular expressions.

We are trying to find in our document the following text:

${Images|maxwidth=100|maxheight=100}
${Images|maxwidth=10|maxheight=90}

(basically the maxheight and maxwidth can vary).

We have developed a IReplacingCallback implementation and used the following code:

final ImageReplaceCallBack a = c.new ImageReplaceCallBack(fields);

result.getRange().replace(Pattern.compile("\\$\\{Images*\\}"), a, true);

But the callback is not being called. Notice that in the document, if we put ${Images}, the callback is called.

Thank you


Hi Paulo,

Thanks for your inquiry. Could you please attach your input Word document here for testing? I will investigate the issue on my side and provide you more information.

Here it goes


Thanks!

Hi Tahir,


We really need help on this, we want to purchase a license during this week, but only after solving this problem.

Best Regards

Hi Paulo,


Thanks for sharing the document. Please change the regular expression as follow in your application. Hope this helps you. Please let us know if you have any more queries.

<span lang=“EN-GB” style=“font-size:
10.0pt;line-height:115%;font-family:“Courier New”;mso-font-kerning:0pt;
mso-no-proof:yes”>doc.getRange().replace(Pattern.compile("\$\{Images*"),
new InsertDocumentAtReplaceHandler(), false);

<span lang=“EN-GB” style=“font-size:
10.0pt;line-height:115%;font-family:“Courier New”;mso-font-kerning:0pt;
mso-no-proof:yes”>

Hi Tahir,


That does not help, as the expression does not detects the rest of the text that I want to replace, i.e. it leaves the |maxwidth=10|maxheight=90} out of the replace.

Check attached file with the result.

Thanks,



Hi Paulo,


Thanks for your inquiry. Please note that all text of the document is stored in Runs of text. The text (${Images|maxwidth=100|maxheight=100}) could be in one or multiple nodes of type Run.

Moreover, the Aspose.Words is quite different from the Microsoft Word’s Object Model in that it represents the document as a tree of objects more like an XML DOM tree. When you load a Word document into Aspose.Words, it builds its DOM and all document elements and formatting are simply loaded into memory. Please read the following articles for more information on DOM:
http://www.aspose.com/docs/display/wordsjava/Object+Model+Overview
http://www.aspose.com/docs/display/wordsjava/Composition+Diagrams

In your scenario, please use the approach to find the Run nodes which contain the matched text at following documentation link. Please check the class ReplaceEvaluatorFindAndHighlight.
http://www.aspose.com/docs/display/wordsjava/How+to++Find+and+Highlight+Text

Hope this helps you. Please let us know if you have any more queries.

Thanks, we used your suggestion and managed to solve the problem iterating the runs.


Kind Regards
Hi Paulo,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.