Tue Jun 07, 2022 6:30 pm
The code you have put works, but in my case I have some more instructions. If you incorporate what I put below into your example code, you will see that it does not work. Basically, if I comment out the getAsOneRange() line in my code it also works, so I think the call to that method does something that causes the end result to be wrong.
My code:
Document document = new Document();
document.loadFromFile("PruebaWord.docx");
Pattern regex=Pattern.compile("<if-(\\w+)>([\\s\\S]*?)((<else-\\1>)([\\s\\S]*?))?(<\\/if-\\1>)",Pattern.CASE_INSENSITIVE);
TextSelection CadenasEncontradas[];
CadenasEncontradas = document.findAllPattern(regex, true);
TextRange Range = CadenasEncontradas[0].getAsOneRange();
document.replace("<if-ifok>", "a", false, true); // Quitamos la instrucion de inicio del if
document.replace("</if-ifok>", "b", false, true);
document.saveToFile("PruebaWord-procesado.docx", FileFormat.Docx);