Aspose::Words::Replacing::ReplacingArgs Class Reference

Detailed Description

Provides data for a custom replace operation.

See also
Aspose::Words::Replacing::IReplacingCallback
Aspose::Words::Range
Aspose::Words::Range::Replace(System::String, System::String, System::SharedPtr<Aspose::Words::Replacing::FindReplaceOptions>)
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 insert an entire document's contents as a replacement of a match in a find-and-replace operation.

void InsertDocumentAtReplace()
{
auto mainDoc = MakeObject<Document>(MyDir + u"Document insertion destination.docx");
// We can use a "FindReplaceOptions" object to modify the find-and-replace process.
auto options = MakeObject<FindReplaceOptions>();
options->set_ReplacingCallback(MakeObject<ExRange::InsertDocumentAtReplaceHandler>());
mainDoc->get_Range()->Replace(MakeObject<System::Text::RegularExpressions::Regex>(u"\\[MY_DOCUMENT\\]"), u"", options);
mainDoc->Save(ArtifactsDir + u"InsertDocument.InsertDocumentAtReplace.docx");
}
class InsertDocumentAtReplaceHandler : public IReplacingCallback
{
public:
ReplaceAction Replacing(SharedPtr<ReplacingArgs> args) override
{
auto subDoc = MakeObject<Document>(MyDir + u"Document.docx");
// Insert a document after the paragraph containing the matched text.
auto para = System::DynamicCast<Paragraph>(args->get_MatchNode()->get_ParentNode());
InsertDocument(para, subDoc);
// Remove the paragraph with the matched text.
para->Remove();
}
};
static void InsertDocument(SharedPtr<Node> insertionDestination, SharedPtr<Document> docToInsert)
{
if (insertionDestination->get_NodeType() == NodeType::Paragraph || insertionDestination->get_NodeType() == NodeType::Table)
{
SharedPtr<CompositeNode> dstStory = insertionDestination->get_ParentNode();
auto importer = MakeObject<NodeImporter>(docToInsert, insertionDestination->get_Document(), ImportFormatMode::KeepSourceFormatting);
for (auto srcSection : System::IterateOver(docToInsert->get_Sections()->LINQ_OfType<SharedPtr<Section>>()))
{
for (auto srcNode : System::IterateOver(srcSection->get_Body()))
{
// Skip the node if it is the last empty paragraph in a section.
if (srcNode->get_NodeType() == NodeType::Paragraph)
{
auto para = System::DynamicCast<Paragraph>(srcNode);
if (para->get_IsEndOfSection() && !para->get_HasChildNodes())
{
continue;
}
}
SharedPtr<Node> newNode = importer->ImportNode(srcNode, true);
dstStory->InsertAfter(newNode, insertionDestination);
insertionDestination = newNode;
}
}
}
else
{
throw System::ArgumentException(u"The destination node must be either a paragraph or table.");
}
}

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

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

Public Member Functions

int32_t get_GroupIndex () const
 Identifies, by index, a captured group in the Match that is to be replaced with the Replacement string. More...
 
String get_GroupName () const
 Identifies, by name, a captured group in the Match that is to be replaced with the Replacement string. More...
 
SharedPtr< Matchget_Match () const
 The Match resulting from a single regular expression match during a Replace. More...
 
SharedPtr< Nodeget_MatchNode () const
 Gets the node that contains the beginning of the match. More...
 
int32_t get_MatchOffset () const
 Gets the zero-based starting position of the match from the start of the node that contains the beginning of the match. More...
 
String get_Replacement () const
 Gets or sets the replacement string. More...
 
virtual const TypeInfoGetType () const override
 
virtual bool Is (const TypeInfo &target) const override
 
void set_GroupIndex (int32_t value)
 Setter for get_GroupIndex. More...
 
void set_GroupName (String value)
 Setter for get_GroupName. More...
 
void set_Replacement (String value)
 Setter for get_Replacement. More...
 

Static Public Member Functions

static const TypeInfoType ()
 

Member Function Documentation

◆ get_GroupIndex()

int32_t Aspose::Words::Replacing::ReplacingArgs::get_GroupIndex ( ) const

Identifies, by index, a captured group in the Match that is to be replaced with the Replacement string.

GroupIndex has effect only when GroupName is null.

Default is zero.

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

System::String Aspose::Words::Replacing::ReplacingArgs::get_GroupName ( ) const

Identifies, by name, a captured group in the Match that is to be replaced with the Replacement string.

When group name is null, GroupIndex is used to identify the group.

Default is null.

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

System::SharedPtr<System::Text::RegularExpressions::Match> Aspose::Words::Replacing::ReplacingArgs::get_Match ( ) const

The Match resulting from a single regular expression match during a Replace.

Match.Index" gets the zero-based starting position of the match from the start of the find and replace range.

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

System::SharedPtr<Aspose::Words::Node> Aspose::Words::Replacing::ReplacingArgs::get_MatchNode ( ) const

Gets the node that contains the beginning of the match.

Examples

Shows how to insert an entire document's contents as a replacement of a match in a find-and-replace operation.

void InsertDocumentAtReplace()
{
auto mainDoc = MakeObject<Document>(MyDir + u"Document insertion destination.docx");
// We can use a "FindReplaceOptions" object to modify the find-and-replace process.
auto options = MakeObject<FindReplaceOptions>();
options->set_ReplacingCallback(MakeObject<ExRange::InsertDocumentAtReplaceHandler>());
mainDoc->get_Range()->Replace(MakeObject<System::Text::RegularExpressions::Regex>(u"\\[MY_DOCUMENT\\]"), u"", options);
mainDoc->Save(ArtifactsDir + u"InsertDocument.InsertDocumentAtReplace.docx");
}
class InsertDocumentAtReplaceHandler : public IReplacingCallback
{
public:
ReplaceAction Replacing(SharedPtr<ReplacingArgs> args) override
{
auto subDoc = MakeObject<Document>(MyDir + u"Document.docx");
// Insert a document after the paragraph containing the matched text.
auto para = System::DynamicCast<Paragraph>(args->get_MatchNode()->get_ParentNode());
InsertDocument(para, subDoc);
// Remove the paragraph with the matched text.
para->Remove();
}
};
static void InsertDocument(SharedPtr<Node> insertionDestination, SharedPtr<Document> docToInsert)
{
if (insertionDestination->get_NodeType() == NodeType::Paragraph || insertionDestination->get_NodeType() == NodeType::Table)
{
SharedPtr<CompositeNode> dstStory = insertionDestination->get_ParentNode();
auto importer = MakeObject<NodeImporter>(docToInsert, insertionDestination->get_Document(), ImportFormatMode::KeepSourceFormatting);
for (auto srcSection : System::IterateOver(docToInsert->get_Sections()->LINQ_OfType<SharedPtr<Section>>()))
{
for (auto srcNode : System::IterateOver(srcSection->get_Body()))
{
// Skip the node if it is the last empty paragraph in a section.
if (srcNode->get_NodeType() == NodeType::Paragraph)
{
auto para = System::DynamicCast<Paragraph>(srcNode);
if (para->get_IsEndOfSection() && !para->get_HasChildNodes())
{
continue;
}
}
SharedPtr<Node> newNode = importer->ImportNode(srcNode, true);
dstStory->InsertAfter(newNode, insertionDestination);
insertionDestination = newNode;
}
}
}
else
{
throw System::ArgumentException(u"The destination node must be either a paragraph or table.");
}
}

◆ get_MatchOffset()

int32_t Aspose::Words::Replacing::ReplacingArgs::get_MatchOffset ( ) const

Gets the zero-based starting position of the match from the start of the node that contains the beginning of the match.

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

System::String Aspose::Words::Replacing::ReplacingArgs::get_Replacement ( ) const

Gets or sets the replacement string.

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;
};

◆ GetType()

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

Reimplemented from System::Object.

◆ Is()

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

Reimplemented from System::Object.

◆ set_GroupIndex()

void Aspose::Words::Replacing::ReplacingArgs::set_GroupIndex ( int32_t  value)

◆ set_GroupName()

void Aspose::Words::Replacing::ReplacingArgs::set_GroupName ( System::String  value)

◆ set_Replacement()

void Aspose::Words::Replacing::ReplacingArgs::set_Replacement ( System::String  value)

◆ Type()

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