正規表現を使用してブック内のテキストを置換する

Aspose.Cells for Python via .NETは、正規表現を使用してワークブック内のテキストを置き換える機能を提供します。これには、APIのReplaceOptions.regex_keyプロパティとReplaceOptionsクラスがあります。ReplaceOptions.regex_keytrueに設定すると、検索キーが正規表現であることを示します。

次のコードスニペットは、ReplaceOptions.regex_key プロパティの使用例を示しています。サンプルExcelファイルを使用して、このプロパティを使用したコードによって生成された出力ファイルが添付されています。

サンプルコード

from aspose.cells import ReplaceOptions, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
# Output directory
outputDir = RunExamples.Get_OutputDirectory()
workbook = Workbook(sourceDir + "SampleRegexReplace.xlsx")
replace = ReplaceOptions()
replace.case_sensitive = False
replace.match_entire_cell_contents = False
# Set to true to indicate that the searched key is regex
replace.regex_key = True
workbook.replace("\\bKIM\\b", "^^^TIM^^^", replace)
workbook.save(outputDir + "RegexReplace_out.xlsx")