使用正则表达式在工作簿中替换文本

Contents
[ ]

Aspose.Cells提供了使用正则表达式在工作簿中替换文本的功能。为此,API提供了RegexKey类的ReplaceOptions属性。将RegexKey设置为true表示搜索的关键字将是正则表达式。

以下代码片段演示了使用RegexKey属性,使用样本Excel文件。以下代码片段生成的输出文件附在下面供参考。

示例代码

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// directories
String sourceDir = Utils.Get_SourceDirectory();
String outputDir = Utils.Get_OutputDirectory();
Workbook workbook = new Workbook(sourceDir + "SampleRegexReplace.xlsx");
ReplaceOptions replace = new ReplaceOptions();
replace.setCaseSensitive(false);
replace.setMatchEntireCellContents(false);
// Set to true to indicate that the searched key is regex
replace.setRegexKey(true);
workbook.replace("\\bKIM\\b", "^^^TIM^^^", replace);
workbook.save(outputDir + "RegexReplace_out.xlsx");