search/mag_sel search/close
Aspose::Words::Replacing::FindReplaceOptions Class Reference

Specifies options for find/replace operations.

Examples

Shows how to toggle case sensitivity when performing a find-and-replace operation.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->Writeln(u"Ruby bought a ruby necklace.");
// We can use a "FindReplaceOptions" object to modify the find-and-replace process.
auto options = MakeObject<FindReplaceOptions>();
// Set the "MatchCase" flag to "true" to apply case sensitivity while finding strings to replace.
// Set the "MatchCase" flag to "false" to ignore character case while searching for text to replace.
options->set_MatchCase(matchCase);
doc->get_Range()->Replace(u"Ruby", u"Jade", options);
ASSERT_EQ(matchCase ? String(u"Jade bought a ruby necklace.") : String(u"Jade bought a Jade necklace."), doc->GetText().Trim());

Shows how to toggle standalone word-only find-and-replace operations.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->Writeln(u"Jackson will meet you in Jacksonville.");
// We can use a "FindReplaceOptions" object to modify the find-and-replace process.
auto options = MakeObject<FindReplaceOptions>();
// Set the "FindWholeWordsOnly" flag to "true" to replace the found text if it is not a part of another word.
// Set the "FindWholeWordsOnly" flag to "false" to replace all text regardless of its surroundings.
options->set_FindWholeWordsOnly(findWholeWordsOnly);
doc->get_Range()->Replace(u"Jackson", u"Louis", options);
ASSERT_EQ(findWholeWordsOnly ? String(u"Louis will meet you in Jacksonville.") : String(u"Louis will meet you in Louisville."), doc->GetText().Trim());

#include <Aspose.Words.Cpp/Replacing/FindReplaceOptions.h>

+ Inheritance diagram for Aspose::Words::Replacing::FindReplaceOptions:

Public Member Functions

 FindReplaceOptions ()
 
 FindReplaceOptions (FindReplaceDirection direction)
 
 FindReplaceOptions (FindReplaceDirection direction, SharedPtr< IReplacingCallback > replacingCallback)
 
 FindReplaceOptions (SharedPtr< IReplacingCallback > replacingCallback)
 
SharedPtr< Fontget_ApplyFont () const
 Text formatting applied to new content. More...
 
SharedPtr< ParagraphFormatget_ApplyParagraphFormat () const
 Paragraph formatting applied to new content. More...
 
FindReplaceDirection get_Direction () const
 Selects direction for replace. Default value is Forward. More...
 
bool get_FindWholeWordsOnly () const
 True indicates the oldValue must be a standalone word. More...
 
bool get_IgnoreDeleted () const
 Gets or sets a boolean value indicating either to ignore text inside delete revisions. The default value is false. More...
 
bool get_IgnoreFields () const
 Gets or sets a boolean value indicating either to ignore text inside fields. The default value is false. More...
 
bool get_IgnoreInserted () const
 Gets or sets a boolean value indicating either to ignore text inside insert revisions. The default value is false. More...
 
bool get_LegacyMode () const
 Gets or sets a boolean value indicating that old find/replace algorithm is used. More...
 
bool get_MatchCase () const
 True indicates case-sensitive comparison, false indicates case-insensitive comparison. More...
 
SharedPtr< IReplacingCallbackget_ReplacingCallback () const
 The user-defined method which is called before every replace occurrence. More...
 
bool get_SmartParagraphBreakReplacement () const
 Gets or sets a boolean value indicating either it is allowed to replace paragraph break when there is no next sibling paragraph. The default value is false. More...
 
bool get_UseLegacyOrder () const
 True indicates that a text search is performed sequentially from top to bottom considering the text boxes. Default value is false. More...
 
bool get_UseSubstitutions () const
 Gets or sets a boolean value indicating whether to recognize and use substitutions within replacement patterns. The default value is false. More...
 
virtual const TypeInfoGetType () const override
 
virtual bool Is (const TypeInfo &target) const override
 
void set_Direction (FindReplaceDirection value)
 Setter for get_Direction. More...
 
void set_FindWholeWordsOnly (bool value)
 Setter for get_FindWholeWordsOnly. More...
 
void set_IgnoreDeleted (bool value)
 Setter for get_IgnoreDeleted. More...
 
void set_IgnoreFields (bool value)
 Setter for get_IgnoreFields. More...
 
void set_IgnoreInserted (bool value)
 Setter for get_IgnoreInserted. More...
 
void set_LegacyMode (bool value)
 Setter for get_LegacyMode. More...
 
void set_MatchCase (bool value)
 Setter for get_MatchCase. More...
 
void set_ReplacingCallback (SharedPtr< IReplacingCallback > value)
 Setter for get_ReplacingCallback. More...
 
void set_SmartParagraphBreakReplacement (bool value)
 Setter for get_SmartParagraphBreakReplacement. More...
 
void set_UseLegacyOrder (bool value)
 Setter for get_UseLegacyOrder. More...
 
void set_UseSubstitutions (bool value)
 Setter for get_UseSubstitutions. More...
 

Static Public Member Functions

static const TypeInfoType ()
 

Constructor & Destructor Documentation

◆ FindReplaceOptions() [1/4]

Aspose::Words::Replacing::FindReplaceOptions::FindReplaceOptions ( )

◆ FindReplaceOptions() [2/4]

Aspose::Words::Replacing::FindReplaceOptions::FindReplaceOptions ( Aspose::Words::Replacing::FindReplaceDirection  direction)

◆ FindReplaceOptions() [3/4]

Aspose::Words::Replacing::FindReplaceOptions::FindReplaceOptions ( System::SharedPtr< Aspose::Words::Replacing::IReplacingCallback replacingCallback)

◆ FindReplaceOptions() [4/4]

Aspose::Words::Replacing::FindReplaceOptions::FindReplaceOptions ( Aspose::Words::Replacing::FindReplaceDirection  direction,
System::SharedPtr< Aspose::Words::Replacing::IReplacingCallback replacingCallback 
)

Member Function Documentation

◆ get_ApplyFont()

System::SharedPtr<Aspose::Words::Font> Aspose::Words::Replacing::FindReplaceOptions::get_ApplyFont ( ) const

Text formatting applied to new content.

Examples

Shows how to apply a different font to new content via FindReplaceOptions.

void ConvertNumbersToHexadecimal()
{
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->get_Font()->set_Name(u"Arial");
builder->Writeln(String(u"Numbers that the find-and-replace operation will convert to hexadecimal and highlight:\n") + u"123, 456, 789 and 17379.");
// We can use a "FindReplaceOptions" object to modify the find-and-replace process.
auto options = MakeObject<FindReplaceOptions>();
// Set the "HighlightColor" property to a background color that we want to apply to the operation's resulting text.
options->get_ApplyFont()->set_HighlightColor(System::Drawing::Color::get_LightGray());
auto numberHexer = MakeObject<ExRange::NumberHexer>();
options->set_ReplacingCallback(numberHexer);
int replacementCount = doc->get_Range()->Replace(MakeObject<System::Text::RegularExpressions::Regex>(u"[0-9]+"), u"", options);
std::cout << numberHexer->GetLog() << std::endl;
ASSERT_EQ(4, replacementCount);
ASSERT_EQ(String(u"Numbers that the find-and-replace operation will convert to hexadecimal and highlight:\r") + u"0x7B, 0x1C8, 0x315 and 0x43E3.",
doc->GetText().Trim());
auto isLightGray = [](SharedPtr<Run> r)
{
return r->get_Font()->get_HighlightColor().ToArgb() == System::Drawing::Color::get_LightGray().ToArgb();
};
ASSERT_EQ(4, doc->GetChildNodes(NodeType::Run, true)->LINQ_OfType<SharedPtr<Run>>()->LINQ_Count(isLightGray));
}
class NumberHexer : public IReplacingCallback
{
public:
ReplaceAction Replacing(SharedPtr<ReplacingArgs> args) override
{
mCurrentReplacementNumber++;
int number = System::Convert::ToInt32(args->get_Match()->get_Value());
args->set_Replacement(String::Format(u"0x{0:X}", number));
mLog->AppendLine(String::Format(u"Match #{0}", mCurrentReplacementNumber));
mLog->AppendLine(String::Format(u"\tOriginal value:\t{0}", args->get_Match()->get_Value()));
mLog->AppendLine(String::Format(u"\tReplacement:\t{0}", args->get_Replacement()));
mLog->AppendLine(String::Format(u"\tOffset in parent {0} node:\t{1}", args->get_MatchNode()->get_NodeType(), args->get_MatchOffset()));
mLog->AppendLine(String::IsNullOrEmpty(args->get_GroupName()) ? String::Format(u"\tGroup index:\t{0}", args->get_GroupIndex())
: String::Format(u"\tGroup name:\t{0}", args->get_GroupName()));
}
String GetLog()
{
return mLog->ToString();
}
NumberHexer() : mCurrentReplacementNumber(0), mLog(MakeObject<System::Text::StringBuilder>())
{
}
private:
int mCurrentReplacementNumber;
SharedPtr<System::Text::StringBuilder> mLog;
};

◆ get_ApplyParagraphFormat()

System::SharedPtr<Aspose::Words::ParagraphFormat> Aspose::Words::Replacing::FindReplaceOptions::get_ApplyParagraphFormat ( ) const

Paragraph formatting applied to new content.

Examples

Shows how to add formatting to paragraphs in which a find-and-replace operation has found matches.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->Writeln(u"Every paragraph that ends with a full stop like this one will be right aligned.");
builder->Writeln(u"This one will not!");
builder->Write(u"This one also will.");
SharedPtr<ParagraphCollection> paragraphs = doc->get_FirstSection()->get_Body()->get_Paragraphs();
ASSERT_EQ(ParagraphAlignment::Left, paragraphs->idx_get(0)->get_ParagraphFormat()->get_Alignment());
ASSERT_EQ(ParagraphAlignment::Left, paragraphs->idx_get(1)->get_ParagraphFormat()->get_Alignment());
ASSERT_EQ(ParagraphAlignment::Left, paragraphs->idx_get(2)->get_ParagraphFormat()->get_Alignment());
// We can use a "FindReplaceOptions" object to modify the find-and-replace process.
auto options = MakeObject<FindReplaceOptions>();
// Set the "Alignment" property to "ParagraphAlignment.Right" to right-align every paragraph
// that contains a match that the find-and-replace operation finds.
options->get_ApplyParagraphFormat()->set_Alignment(ParagraphAlignment::Right);
// Replace every full stop that is right before a paragraph break with an exclamation point.
int count = doc->get_Range()->Replace(u".&p", u"!&p", options);
ASSERT_EQ(2, count);
ASSERT_EQ(ParagraphAlignment::Right, paragraphs->idx_get(0)->get_ParagraphFormat()->get_Alignment());
ASSERT_EQ(ParagraphAlignment::Left, paragraphs->idx_get(1)->get_ParagraphFormat()->get_Alignment());
ASSERT_EQ(ParagraphAlignment::Right, paragraphs->idx_get(2)->get_ParagraphFormat()->get_Alignment());
ASSERT_EQ(String(u"Every paragraph that ends with a full stop like this one will be right aligned!\r") + u"This one will not!\r" +
u"This one also will!",
doc->GetText().Trim());

◆ get_Direction()

Aspose::Words::Replacing::FindReplaceDirection Aspose::Words::Replacing::FindReplaceOptions::get_Direction ( ) const

Selects direction for replace. Default value is Forward.

Examples

Shows how to determine which direction a find-and-replace operation traverses the document in.

void Direction(FindReplaceDirection findReplaceDirection)
{
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
// Insert three runs which we can search for using a regex pattern.
// Place one of those runs inside a text box.
builder->Writeln(u"Match 1.");
builder->Writeln(u"Match 2.");
builder->Writeln(u"Match 3.");
builder->Writeln(u"Match 4.");
// We can use a "FindReplaceOptions" object to modify the find-and-replace process.
auto options = MakeObject<FindReplaceOptions>();
// Assign a custom callback to the "ReplacingCallback" property.
auto callback = MakeObject<ExRange::TextReplacementRecorder>();
options->set_ReplacingCallback(callback);
// Set the "Direction" property to "FindReplaceDirection.Backward" to get the find-and-replace
// operation to start from the end of the range, and traverse back to the beginning.
// Set the "Direction" property to "FindReplaceDirection.Backward" to get the find-and-replace
// operation to start from the beginning of the range, and traverse to the end.
options->set_Direction(findReplaceDirection);
doc->get_Range()->Replace(MakeObject<System::Text::RegularExpressions::Regex>(u"Match \\d*"), u"Replacement", options);
ASSERT_EQ(String(u"Replacement.\r") + u"Replacement.\r" + u"Replacement.\r" + u"Replacement.", doc->GetText().Trim());
switch (findReplaceDirection)
{
ASPOSE_ASSERT_EQ(MakeArray<String>({u"Match 1", u"Match 2", u"Match 3", u"Match 4"}), callback->get_Matches());
break;
ASPOSE_ASSERT_EQ(MakeArray<String>({u"Match 4", u"Match 3", u"Match 2", u"Match 1"}), callback->get_Matches());
break;
}
}
class TextReplacementRecorder : public IReplacingCallback
{
public:
SharedPtr<System::Collections::Generic::List<String>> get_Matches()
{
return mMatches;
}
ReplaceAction Replacing(SharedPtr<ReplacingArgs> e) override
{
get_Matches()->Add(e->get_Match()->get_Value());
}
TextReplacementRecorder() : mMatches(MakeObject<System::Collections::Generic::List<String>>())
{
}
private:
SharedPtr<System::Collections::Generic::List<String>> mMatches;
};

◆ get_FindWholeWordsOnly()

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

True indicates the oldValue must be a standalone word.

Examples

Shows how to toggle standalone word-only find-and-replace operations.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->Writeln(u"Jackson will meet you in Jacksonville.");
// We can use a "FindReplaceOptions" object to modify the find-and-replace process.
auto options = MakeObject<FindReplaceOptions>();
// Set the "FindWholeWordsOnly" flag to "true" to replace the found text if it is not a part of another word.
// Set the "FindWholeWordsOnly" flag to "false" to replace all text regardless of its surroundings.
options->set_FindWholeWordsOnly(findWholeWordsOnly);
doc->get_Range()->Replace(u"Jackson", u"Louis", options);
ASSERT_EQ(findWholeWordsOnly ? String(u"Louis will meet you in Jacksonville.") : String(u"Louis will meet you in Louisville."), doc->GetText().Trim());

◆ get_IgnoreDeleted()

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

Gets or sets a boolean value indicating either to ignore text inside delete revisions. The default value is false.

Examples

Shows how to include or ignore text inside delete revisions during a find-and-replace operation.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->Writeln(u"Hello world!");
builder->Writeln(u"Hello again!");
// Start tracking revisions and remove the second paragraph, which will create a delete revision.
// That paragraph will persist in the document until we accept the delete revision.
doc->StartTrackRevisions(u"John Doe", System::DateTime::get_Now());
doc->get_FirstSection()->get_Body()->get_Paragraphs()->idx_get(1)->Remove();
doc->StopTrackRevisions();
ASSERT_TRUE(doc->get_FirstSection()->get_Body()->get_Paragraphs()->idx_get(1)->get_IsDeleteRevision());
// We can use a "FindReplaceOptions" object to modify the find and replace process.
auto options = MakeObject<FindReplaceOptions>();
// Set the "IgnoreDeleted" flag to "true" to get the find-and-replace
// operation to ignore paragraphs that are delete revisions.
// Set the "IgnoreDeleted" flag to "false" to get the find-and-replace
// operation to also search for text inside delete revisions.
options->set_IgnoreDeleted(ignoreTextInsideDeleteRevisions);
doc->get_Range()->Replace(u"Hello", u"Greetings", options);
ASSERT_EQ(ignoreTextInsideDeleteRevisions ? String(u"Greetings world!\rHello again!") : String(u"Greetings world!\rGreetings again!"),
doc->GetText().Trim());

◆ get_IgnoreFields()

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

Gets or sets a boolean value indicating either to ignore text inside fields. The default value is false.

Examples

Shows how to ignore text inside fields.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->Writeln(u"Hello world!");
builder->InsertField(u"QUOTE", u"Hello again!");
// We can use a "FindReplaceOptions" object to modify the find-and-replace process.
auto options = MakeObject<FindReplaceOptions>();
// Set the "IgnoreFields" flag to "true" to get the find-and-replace
// operation to ignore text inside fields.
// Set the "IgnoreFields" flag to "false" to get the find-and-replace
// operation to also search for text inside fields.
options->set_IgnoreFields(ignoreTextInsideFields);
doc->get_Range()->Replace(u"Hello", u"Greetings", options);
ASSERT_EQ(ignoreTextInsideFields ? String(u"Greetings world!\r\u0013QUOTE\u0014Hello again!\u0015")
: String(u"Greetings world!\r\u0013QUOTE\u0014Greetings again!\u0015"),
doc->GetText().Trim());

◆ get_IgnoreInserted()

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

Gets or sets a boolean value indicating either to ignore text inside insert revisions. The default value is false.

Examples

Shows how to include or ignore text inside insert revisions during a find-and-replace operation.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->Writeln(u"Hello world!");
// Start tracking revisions and insert a paragraph. That paragraph will be an insert revision.
doc->StartTrackRevisions(u"John Doe", System::DateTime::get_Now());
builder->Writeln(u"Hello again!");
doc->StopTrackRevisions();
ASSERT_TRUE(doc->get_FirstSection()->get_Body()->get_Paragraphs()->idx_get(1)->get_IsInsertRevision());
// We can use a "FindReplaceOptions" object to modify the find-and-replace process.
auto options = MakeObject<FindReplaceOptions>();
// Set the "IgnoreInserted" flag to "true" to get the find-and-replace
// operation to ignore paragraphs that are insert revisions.
// Set the "IgnoreInserted" flag to "false" to get the find-and-replace
// operation to also search for text inside insert revisions.
options->set_IgnoreInserted(ignoreTextInsideInsertRevisions);
doc->get_Range()->Replace(u"Hello", u"Greetings", options);
ASSERT_EQ(ignoreTextInsideInsertRevisions ? String(u"Greetings world!\rHello again!") : String(u"Greetings world!\rGreetings again!"),
doc->GetText().Trim());

◆ get_LegacyMode()

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

Gets or sets a boolean value indicating that old find/replace algorithm is used.

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");

◆ get_MatchCase()

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

True indicates case-sensitive comparison, false indicates case-insensitive comparison.

Examples

Shows how to toggle case sensitivity when performing a find-and-replace operation.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->Writeln(u"Ruby bought a ruby necklace.");
// We can use a "FindReplaceOptions" object to modify the find-and-replace process.
auto options = MakeObject<FindReplaceOptions>();
// Set the "MatchCase" flag to "true" to apply case sensitivity while finding strings to replace.
// Set the "MatchCase" flag to "false" to ignore character case while searching for text to replace.
options->set_MatchCase(matchCase);
doc->get_Range()->Replace(u"Ruby", u"Jade", options);
ASSERT_EQ(matchCase ? String(u"Jade bought a ruby necklace.") : String(u"Jade bought a Jade necklace."), doc->GetText().Trim());

◆ get_ReplacingCallback()

System::SharedPtr<Aspose::Words::Replacing::IReplacingCallback> Aspose::Words::Replacing::FindReplaceOptions::get_ReplacingCallback ( ) const

The user-defined method which is called before every replace occurrence.

Examples

Shows how to replace all occurrences of a regular expression pattern with another string, while tracking all such replacements.

void ReplaceWithCallback()
{
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->Writeln(String(u"Our new location in New York City is opening tomorrow. ") + u"Hope to see all our NYC-based customers at the opening!");
// We can use a "FindReplaceOptions" object to modify the find-and-replace process.
auto options = MakeObject<FindReplaceOptions>();
// Set a callback that tracks any replacements that the "Replace" method will make.
auto logger = MakeObject<ExRange::TextFindAndReplacementLogger>();
options->set_ReplacingCallback(logger);
doc->get_Range()->Replace(MakeObject<System::Text::RegularExpressions::Regex>(u"New York City|NYC"), u"Washington", options);
ASSERT_EQ(String(u"Our new location in (Old value:\"New York City\") Washington is opening tomorrow. ") +
u"Hope to see all our (Old value:\"NYC\") Washington-based customers at the opening!",
doc->GetText().Trim());
ASSERT_EQ(String(u"\"New York City\" converted to \"Washington\" 20 characters into a Run node.\r\n") +
u"\"NYC\" converted to \"Washington\" 42 characters into a Run node.",
logger->GetLog().Trim());
}
class TextFindAndReplacementLogger : public IReplacingCallback
{
public:
ReplaceAction Replacing(SharedPtr<ReplacingArgs> args) override
{
mLog->AppendLine(String::Format(u"\"{0}\" converted to \"{1}\" ", args->get_Match()->get_Value(), args->get_Replacement()) +
String::Format(u"{0} characters into a {1} node.", args->get_MatchOffset(), args->get_MatchNode()->get_NodeType()));
args->set_Replacement(String::Format(u"(Old value:\"{0}\") {1}", args->get_Match()->get_Value(), args->get_Replacement()));
}
String GetLog()
{
return mLog->ToString();
}
TextFindAndReplacementLogger() : mLog(MakeObject<System::Text::StringBuilder>())
{
}
private:
SharedPtr<System::Text::StringBuilder> mLog;
};

Shows how to apply a different font to new content via FindReplaceOptions.

void ConvertNumbersToHexadecimal()
{
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->get_Font()->set_Name(u"Arial");
builder->Writeln(String(u"Numbers that the find-and-replace operation will convert to hexadecimal and highlight:\n") + u"123, 456, 789 and 17379.");
// We can use a "FindReplaceOptions" object to modify the find-and-replace process.
auto options = MakeObject<FindReplaceOptions>();
// Set the "HighlightColor" property to a background color that we want to apply to the operation's resulting text.
options->get_ApplyFont()->set_HighlightColor(System::Drawing::Color::get_LightGray());
auto numberHexer = MakeObject<ExRange::NumberHexer>();
options->set_ReplacingCallback(numberHexer);
int replacementCount = doc->get_Range()->Replace(MakeObject<System::Text::RegularExpressions::Regex>(u"[0-9]+"), u"", options);
std::cout << numberHexer->GetLog() << std::endl;
ASSERT_EQ(4, replacementCount);
ASSERT_EQ(String(u"Numbers that the find-and-replace operation will convert to hexadecimal and highlight:\r") + u"0x7B, 0x1C8, 0x315 and 0x43E3.",
doc->GetText().Trim());
auto isLightGray = [](SharedPtr<Run> r)
{
return r->get_Font()->get_HighlightColor().ToArgb() == System::Drawing::Color::get_LightGray().ToArgb();
};
ASSERT_EQ(4, doc->GetChildNodes(NodeType::Run, true)->LINQ_OfType<SharedPtr<Run>>()->LINQ_Count(isLightGray));
}
class NumberHexer : public IReplacingCallback
{
public:
ReplaceAction Replacing(SharedPtr<ReplacingArgs> args) override
{
mCurrentReplacementNumber++;
int number = System::Convert::ToInt32(args->get_Match()->get_Value());
args->set_Replacement(String::Format(u"0x{0:X}", number));
mLog->AppendLine(String::Format(u"Match #{0}", mCurrentReplacementNumber));
mLog->AppendLine(String::Format(u"\tOriginal value:\t{0}", args->get_Match()->get_Value()));
mLog->AppendLine(String::Format(u"\tReplacement:\t{0}", args->get_Replacement()));
mLog->AppendLine(String::Format(u"\tOffset in parent {0} node:\t{1}", args->get_MatchNode()->get_NodeType(), args->get_MatchOffset()));
mLog->AppendLine(String::IsNullOrEmpty(args->get_GroupName()) ? String::Format(u"\tGroup index:\t{0}", args->get_GroupIndex())
: String::Format(u"\tGroup name:\t{0}", args->get_GroupName()));
}
String GetLog()
{
return mLog->ToString();
}
NumberHexer() : mCurrentReplacementNumber(0), mLog(MakeObject<System::Text::StringBuilder>())
{
}
private:
int mCurrentReplacementNumber;
SharedPtr<System::Text::StringBuilder> mLog;
};

◆ get_SmartParagraphBreakReplacement()

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

Gets or sets a boolean value indicating either it is allowed to replace paragraph break when there is no next sibling paragraph. The default value is false.

Examples

Shows how to remove paragraph from a table cell with a nested table.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
// Create table with paragraph and inner table in first cell.
builder->StartTable();
builder->InsertCell();
builder->Write(u"TEXT1");
builder->StartTable();
builder->InsertCell();
builder->EndTable();
builder->EndTable();
builder->Writeln();
auto options = MakeObject<FindReplaceOptions>();
// When the following option is set to 'true', Aspose.Words will remove paragraph's text
// completely with its paragraph mark. Otherwise, Aspose.Words will mimic Word and remove
// only paragraph's text and leaves the paragraph mark intact (when a table follows the text).
options->set_SmartParagraphBreakReplacement(isSmartParagraphBreakReplacement);
doc->get_Range()->Replace(MakeObject<System::Text::RegularExpressions::Regex>(u"TEXT1&p"), u"", options);
doc->Save(ArtifactsDir + u"Table.RemoveParagraphTextAndMark.docx");

◆ get_UseLegacyOrder()

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

True indicates that a text search is performed sequentially from top to bottom considering the text boxes. Default value is false.

Examples

Shows how to change the searching order of nodes when performing a find-and-replace text operation.

void UseLegacyOrder(bool useLegacyOrder)
{
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
// Insert three runs which we can search for using a regex pattern.
// Place one of those runs inside a text box.
builder->Writeln(u"[tag 1]");
SharedPtr<Shape> textBox = builder->InsertShape(ShapeType::TextBox, 100, 50);
builder->Writeln(u"[tag 2]");
builder->MoveTo(textBox->get_FirstParagraph());
builder->Write(u"[tag 3]");
// We can use a "FindReplaceOptions" object to modify the find-and-replace process.
auto options = MakeObject<FindReplaceOptions>();
// Assign a custom callback to the "ReplacingCallback" property.
auto callback = MakeObject<ExRange::TextReplacementTracker>();
options->set_ReplacingCallback(callback);
// If we set the "UseLegacyOrder" property to "true", the
// find-and-replace operation will go through all the runs outside of a text box
// before going through the ones inside a text box.
// If we set the "UseLegacyOrder" property to "false", the
// find-and-replace operation will go over all the runs in a range in sequential order.
options->set_UseLegacyOrder(useLegacyOrder);
doc->get_Range()->Replace(MakeObject<System::Text::RegularExpressions::Regex>(u"\\[tag \\d*\\]"), u"", options);
SharedPtr<System::Collections::Generic::List<String>> matches = MakeObject<System::Collections::Generic::List<String>>();
matches->Add(u"[tag 1]");
if (useLegacyOrder)
{
matches->Add(u"[tag 3]");
matches->Add(u"[tag 2]");
}
else
{
matches->Add(u"[tag 2]");
matches->Add(u"[tag 3]");
}
ASPOSE_ASSERT_EQ(matches, callback->get_Matches());
}
class TextReplacementTracker : public IReplacingCallback
{
public:
SharedPtr<System::Collections::Generic::List<String>> get_Matches()
{
return mMatches;
}
ReplaceAction Replacing(SharedPtr<ReplacingArgs> e) override
{
get_Matches()->Add(e->get_Match()->get_Value());
}
TextReplacementTracker() : mMatches(MakeObject<System::Collections::Generic::List<String>>())
{
}
private:
SharedPtr<System::Collections::Generic::List<String>> mMatches;
};

◆ get_UseSubstitutions()

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

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

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());

◆ GetType()

virtual const System::TypeInfo& Aspose::Words::Replacing::FindReplaceOptions::GetType ( ) const
overridevirtual

Reimplemented from System::Object.

◆ Is()

virtual bool Aspose::Words::Replacing::FindReplaceOptions::Is ( const System::TypeInfo target) const
overridevirtual

Reimplemented from System::Object.

◆ set_Direction()

void Aspose::Words::Replacing::FindReplaceOptions::set_Direction ( Aspose::Words::Replacing::FindReplaceDirection  value)

◆ set_FindWholeWordsOnly()

void Aspose::Words::Replacing::FindReplaceOptions::set_FindWholeWordsOnly ( bool  value)

◆ set_IgnoreDeleted()

void Aspose::Words::Replacing::FindReplaceOptions::set_IgnoreDeleted ( bool  value)

◆ set_IgnoreFields()

void Aspose::Words::Replacing::FindReplaceOptions::set_IgnoreFields ( bool  value)

◆ set_IgnoreInserted()

void Aspose::Words::Replacing::FindReplaceOptions::set_IgnoreInserted ( bool  value)

◆ set_LegacyMode()

void Aspose::Words::Replacing::FindReplaceOptions::set_LegacyMode ( bool  value)

◆ set_MatchCase()

void Aspose::Words::Replacing::FindReplaceOptions::set_MatchCase ( bool  value)

◆ set_ReplacingCallback()

void Aspose::Words::Replacing::FindReplaceOptions::set_ReplacingCallback ( System::SharedPtr< Aspose::Words::Replacing::IReplacingCallback value)

◆ set_SmartParagraphBreakReplacement()

void Aspose::Words::Replacing::FindReplaceOptions::set_SmartParagraphBreakReplacement ( bool  value)

◆ set_UseLegacyOrder()

void Aspose::Words::Replacing::FindReplaceOptions::set_UseLegacyOrder ( bool  value)

◆ set_UseSubstitutions()

void Aspose::Words::Replacing::FindReplaceOptions::set_UseSubstitutions ( bool  value)

◆ Type()

static const System::TypeInfo& Aspose::Words::Replacing::FindReplaceOptions::Type ( )
static