Aspose::Words::Replacing::FindReplaceOptions::get_UseSubstitutions method

FindReplaceOptions::get_UseSubstitutions method

Gets or sets a boolean value indicating whether to recognize and use substitutions within replacement patterns. The default value is false.

bool Aspose::Words::Replacing::FindReplaceOptions::get_UseSubstitutions() const

Examples

Shows how to recognize and use substitutions within replacement patterns.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);

builder->Write(u"Jason gave money to Paul.");

auto regex = MakeObject<System::Text::RegularExpressions::Regex>(u"([A-z]+) gave money to ([A-z]+)");

auto options = MakeObject<FindReplaceOptions>();
options->set_UseSubstitutions(true);

// Using legacy mode does not support many advanced features, so we need to set it to 'false'.
options->set_LegacyMode(false);

doc->get_Range()->Replace(regex, u"$2 took money from $1", options);

ASSERT_EQ(doc->GetText(), u"Paul took money from Jason.\f");

Shows how to replace the text with substitutions.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);

builder->Writeln(u"John sold a car to Paul.");
builder->Writeln(u"Jane sold a house to Joe.");

// We can use a "FindReplaceOptions" object to modify the find-and-replace process.
auto options = MakeObject<FindReplaceOptions>();

// Set the "UseSubstitutions" property to "true" to get
// the find-and-replace operation to recognize substitution elements.
// Set the "UseSubstitutions" property to "false" to ignore substitution elements.
options->set_UseSubstitutions(useSubstitutions);

auto regex = MakeObject<System::Text::RegularExpressions::Regex>(u"([A-z]+) sold a ([A-z]+) to ([A-z]+)");
doc->get_Range()->Replace(regex, u"$3 bought a $2 from $1", options);

ASSERT_EQ(useSubstitutions ? String(u"Paul bought a car from John.\rJoe bought a house from Jane.")
                           : String(u"$3 bought a $2 from $1.\r$3 bought a $2 from $1."),
          doc->GetText().Trim());

See Also