使用正则表达式在工作簿中替换文本
Contents
[
Hide
]
Aspose.Cells提供了使用正则表达式在工作簿中替换文本的功能。为此,API提供了ReplaceOptions类的RegexKey属性。将RegexKey设置为true表示搜索的键将是一个正则表达式。
以下代码片段演示了通过使用示例Excel文件使用RegexKey属性。以下代码片段生成的输出文件附在此供参考。
示例代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
//Output directory | |
string outputDir = RunExamples.Get_OutputDirectory(); | |
Workbook workbook = new Workbook(sourceDir + "SampleRegexReplace.xlsx"); | |
ReplaceOptions replace = new ReplaceOptions(); | |
replace.CaseSensitive = false; | |
replace.MatchEntireCellContents = false; | |
// Set to true to indicate that the searched key is regex | |
replace.RegexKey = true; | |
workbook.Replace("\\bKIM\\b", "^^^TIM^^^", replace); | |
workbook.Save(outputDir + "RegexReplace_out.xlsx"); |