Why can i get run like [xx-xx]

the testing text is ‘此外,该办公空间空调系统通常存在间歇运行,而间歇运行需要考虑围护结构及各类末端的热惯性,即使均匀环境的间歇负荷,也较为复杂[35-37]。’ but I cant get the [35-37] using paragraph.getText(), but I want to use setSuperscript to the [xx-xx] , How can i do that ?

@Madecho You can easily achieve this using Find/Replace functionality. In your case you can use substitutions and apply font. Please see the following code:

Document doc = new Document("C:\\Temp\\in.docx");
FindReplaceOptions opt = new FindReplaceOptions();
opt.setUseSubstitutions(true);
opt.getApplyFont().setSuperscript(true);
doc.getRange().replace("[35-37]", "$0", opt);
doc.save("C:\\Temp\\out.docx");

wwww ty alexey! you guys are the best after sale service engineers that I ever met !

1 Like

@alexey.noskov hi alexey, why i cant replace using regex ? . the testing paragraph is

案例二为敞开式大平大小不同[34]。当保障房间局部需求时[31,32,33],只需开启相对应的末端。因此,若想获得保,即使均匀环境的间歇负荷,也较为复杂[35-37]。?

and my code is

FindReplaceOptions opt = new FindReplaceOptions();
        opt.setUseSubstitutions(true);
        opt.getApplyFont().setSuperscript(true);
        doc.getRange().replace("\\[\\d+\\]", "$0", opt);
        doc.getRange().replace("\\[(\\d+,)+\\d+\\]", "$0", opt);
        doc.getRange().replace("\\[\\d+-\\d+\\]", "$0", opt);

however, nothing changed. why ? Did I miss something ?

@Madecho You can. You should simply pass the Pattern as the first argument into replace method. Please modify your code like this:

Document doc = new Document("C:\\Temp\\in.docx");
FindReplaceOptions opt = new FindReplaceOptions();
opt.setUseSubstitutions(true);
opt.getApplyFont().setSuperscript(true);
doc.getRange().replace(Pattern.compile("\\[\\d+\\]"), "$0", opt);
doc.getRange().replace(Pattern.compile("\\[(\\d+,)+\\d+\\]"), "$0", opt);
doc.getRange().replace(Pattern.compile("\\[\\d+-\\d+\\]"), "$0", opt);
doc.save("C:\\Temp\\out.docx");
1 Like