正規表現を使用してワークブック内のテキストを置換するC++コード

Aspose.Cellsは、正規表現を使ってワークブック内のテキストを置換する機能を提供しています。これには、APIのGetRegexKey()プロパティとReplaceOptionsクラスを使用します。GetRegexKey()trueに設定すると、検索キーが正規表現であることを示します。

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

サンプルコード

#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;

int main()
{
    Aspose::Cells::Startup();

    // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C

    // Source directory path
    U16String sourceDir = u"..\\Data\\01_SourceDirectory\\";

    // Output directory path
    U16String outputDir = u"..\\Data\\02_OutputDirectory\\";

    // Create workbook from the input file
    Workbook workbook(sourceDir + u"SampleRegexReplace.xlsx");

    // Create replace options
    ReplaceOptions replace;
    replace.SetCaseSensitive(false);
    replace.SetMatchEntireCellContents(false);
    // Set to true to indicate that the searched key is regex
    replace.SetRegexKey(true);

    // Perform the regex replace operation
    workbook.Replace(u"\\bKIM\\b", u"^^^TIM^^^", replace);

    // Save the modified workbook
    workbook.Save(outputDir + u"RegexReplace_out.xlsx");

    std::cout << "Regex replace operation completed successfully!" << std::endl;

    Aspose::Cells::Cleanup();
}