Replace method not working in 23.1 version

Hi,

I was using 14.7 version of aspose, due to latest .word.lic file doesnot support older version so i updated it to 23.1 version. now my existing code for split pattern is not working.

public void IsolateTokenInForm(com.aspose.words.Document asposeDoc,
                            double formPackageVersion) throws Exception {
    if (formPackageVersion < 1) {
        try {
            asposeDoc.getRange().replace(DEFAULT_TOKEN_PATTERN, "", new FindReplaceOptions(new SplitPatternToIsolatedNode()));


        } catch (Exception e) {
            logger.warn("Error in splitting pattern to isolate");
            throw e;
        }
    }
}
private class SplitPatternToIsolatedNode implements IReplacingCallback {
    // / This method is called by the Aspose.Words find and replace engine
    // for each match.
    // / This method highlights the match string, even if it spans multiple
    // runs.
    @Override
    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.
        // JAM: gets the zero-based starting position of the match from the
        // start of the node that contains the beginning of the match.
        if (e.getMatchOffset() > 0) {
            currentNode = SplitRun((Run) currentNode, e.getMatchOffset());
           
        }

        // This array is used to store all nodes of the match for further
        // highlighting.
        // This can happen if some formmating or something else causes
        // search result to split in to 2 or mode nodes.
        ArrayList<Run> runs = new ArrayList<Run>();

        // Find all runs that contain parts of the match string.
        // JAM: getMatch() returns a java.util.regex.Matcher
        // resulting from a single regular expression match during a Replace
        int remainingLength = e.getMatch().group().length();
        while (remainingLength > 0 && currentNode != null
                && currentNode.getText().length() <= remainingLength) {
            runs.add((Run) 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((Run) currentNode);
        }

        if (runs.size() > 1) {
            MergeRuns(runs);
        }
        return ReplaceAction.SKIP;
    }
}

// Split any run at the given position
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, position));
    run.getParentNode().insertAfter(afterRun, run);
       
    return afterRun;
}

// Merge the arraylist of runs
private void MergeRuns(ArrayList<Run> runs) throws Exception {
    for (int i = 1; i < runs.size(); i++) {
        runs.get(0).setText(runs.get(0).getText() + runs.get(i).getText());
        runs.get(i).setText("");
    }
}

@meghaasati Could you please attach your input, output and expected output documents here for our reference? Also, please, provide your DEFAULT_TOKEN_PATTERN regular expression. We will cehck the issue and provide you more information.

Hi Alexey,

public static Pattern DEFAULT_TOKEN_PATTERN = Pattern.compile("~([ABCDEFNILMPQRT,;][A-Z0-9/\\.@]{4}(\\[.*?\\])?[GVRSE])"); 

This is the pattern.
Is it possible to get licence file for 14.7.0 version.
please find the attached document for input data and output data.data.zip (99.6 KB)

@meghaasati Please use backward find/replace direction to fix the problem:

FindReplaceOptions opt = new FindReplaceOptions(FindReplaceDirection.BACKWARD);

By the way with the latest version of Aspose.Words you can achieve the same without IReplacingCallback:

Document doc = new Document("C:\\Temp\\in.rtf");
FindReplaceOptions opt = new FindReplaceOptions();
opt.setUseSubstitutions(true);
doc.getRange().replace(DEFAULT_TOKEN_PATTERN, "$0", opt);
doc.save("C:\\Temp\\out.docx");

Please contact our sales team in Aspose.Purchase forum:
https://forum.aspose.com/c/purchase/6

Hi Alexey,

Thank you for the help. Now I am able to proceed the document, however output is still mismatch due to that further steps of workflow are failing. I have attached input, actual output and expected output files for example.output.zip (22.3 KB)

@meghaasati Unfortunately, I cannot reproduce the problem on my side. Here is the output document produced on my side using the following code:

Document doc = new Document("C:\\Temp\\in.rtf");
FindReplaceOptions opt = new FindReplaceOptions(FindReplaceDirection.BACKWARD);
opt.setReplacingCallback(new SplitPatternToIsolatedNode());
doc.getRange().replace(DEFAULT_TOKEN_PATTERN, "", opt);
doc.save("C:\\Temp\\out.xml", SaveFormat.FLAT_OPC);

out1.zip (7.4 KB)
And here is the output produced by the following code:

Document doc = new Document("C:\\Temp\\in.rtf");
FindReplaceOptions opt = new FindReplaceOptions();
opt.setUseSubstitutions(true);
doc.getRange().replace(DEFAULT_TOKEN_PATTERN, "$0", opt);
doc.save("C:\\Temp\\out.xml", SaveFormat.FLAT_OPC);

out2.zip (7.4 KB)