Get Node that Contains the End of Matched String (Java) | Find and Replace Keywords in Word DOCX ODT Documents

FindReplaceOptions optionsWhile = new FindReplaceOptions();
optionsWhile.setReplacingCallback(new ReplaceHandler());
WordDoc.getRange().replace(Pattern.compile("[tag(.+?)](.+?)[/tag]"), "", optionsWhile);

in ReplaceHandler we can use args.getMatchNode() to gets the node that contains the beginning of the match, how to gets the node that contains the end of the match?
or how to get the all node from match and repeat in the document?

thanks

@hlgao,

You can meet this requirement by using the following code of Aspose.Words for Java:

Document doc = new Document("C:\\temp\\find replace test.docx");

ReplaceHandler handler = new ReplaceHandler();
FindReplaceOptions opts = new FindReplaceOptions();
opts.setDirection(FindReplaceDirection.BACKWARD);
opts.setReplacingCallback(handler);

doc.getRange().replace("pattern", "", opts);

doc.save("C:\\temp\\SuperscriptIssue\\awjava-20.11.docx");

  static class ReplaceHandler implements IReplacingCallback {

    public int replacing(ReplacingArgs e) throws Exception {
        // This is a Run node that contains either the beginning or the complete match.
        Node currentNode = e.getMatchNode();

        // The first (and may be the only) run can contain text before the match,
        // in this case it is necessary to split the run.
        if (e.getMatchOffset() > 0)
            currentNode = splitRun((Run) currentNode, e.getMatchOffset());

        ArrayList runs = new ArrayList();

        // Find all runs that contain parts of the match string.
        int remainingLength = e.getMatch().group().length();
        while ((remainingLength > 0) && (currentNode != null) && (currentNode.getText().length() <= remainingLength)) {
            runs.add(currentNode);
            remainingLength = remainingLength - currentNode.getText().length();

            // Select the next Run node.
            // Have to loop because there could be other nodes such as BookmarkStart etc.
            do {
                currentNode = currentNode.getNextSibling();
            } while ((currentNode != null) && (currentNode.getNodeType() != NodeType.RUN));
        }

        // Split the last run that contains the match if there is any text left.
        if ((currentNode != null) && (remainingLength > 0)) {
            splitRun((Run) currentNode, remainingLength);
            runs.add(currentNode);
        }

        Run lastNode = (Run) runs.get(runs.size() - 1);
        System.out.println(lastNode.getText());
        for (int i = 0; i < runs.size(); i++)
            System.out.print(((Run) runs.get(i)).getText());

        // Signal to the replace engine to do nothing because we have already done all what we wanted.
        return ReplaceAction.SKIP;
    }

    /**
     * Splits text of the specified run into two runs. Inserts the new run just
     * after the specified run.
     */
    private Run splitRun(Run run, int position) throws Exception {
        Run afterRun = (Run) run.deepClone(true);
        afterRun.setText(run.getText().substring(position));
        run.setText(run.getText().substring((0), (0) + (position)));
        run.getParentNode().insertAfter(afterRun, run);
        return afterRun;
    }
}

@awais.hafeez
Thanks a lot