查找和替换是MS Word提供的一项有用功能,可以一次更新所有出现的特定文本。因此,不必手动查找和替换整个文档中的文本。
在本文中,将学习如何在C ++应用程序中查找和替换Word文档中的文本。当需要一次性替换多个文档中的特定文本时,这可能会很有用。
- 查找和替换Word文档中的文本
- 使用正则表达式查找和替换文本
- 使用元字符替换文本
- 在查找和替换操作过程中忽略文本
Aspose.Words for C ++最新下载(qun:761297826)https://www.evget.com/product/4114
使用C ++查找和替换Word文档中的文本
以下是使用Aspose.Words for C ++在Word文档中查找和替换文本的步骤。
- 使用Document类加载Word文档。
- 使用Document-> get_Range()-> Replace(u“ sad”,u“ bad”,System :: MakeObject < FindReplaceOptions >(FindReplaceDirection :: Forward))方法替换所需的单词。
- 使用Document-> Save(String)方法保存更新的Word文档。
下面的代码示例演示如何使用C ++在Word文档中查找“ sad”并将其替换为“ bad”。
// Load MS Word document System::SharedPtrdoc = System::MakeObject(u"Document.doc"); // Find and replace the text doc->get_Range()->Replace(u"sad", u"bad", System::MakeObject(FindReplaceDirection::Forward)); // Save the updated document doc->Save(u"updated.doc");
使用正则表达式查找和替换文本
可以定义正则表达式,以查找和替换遵循特定模式的单词。例如,您可以将单词“ sad”和“ mad”替换为“ bad”。以下是在Word文档中查找和替换与正则表达式匹配的单词的步骤。
- 使用Document类加载Word文档。
- 使用Regex类定义正则表达式。
- 使用Document-> get_Range()-> Replace替换所需的单词(System :: MakeObject < System :: Text :: RegularExpressions :: Regex >(u” [s | m] ad”),u” bad”,选项)方法。
- 使用Document-> Save(String)方法保存更新的Word文档。
下面的代码示例演示如何在C ++中使用正则表达式查找和替换文本。
// Load MS Word document System::SharedPtrdoc = System::MakeObject(u"Document.doc"); // Create find and replace options System::SharedPtroptions = System::MakeObject(); // Find and replace the text doc->get_Range()->Replace(System::MakeObject(u"[s|m]ad"), u"bad", options); // Save the updated document doc->Save(u"updated.doc");
使用元字符查找和替换文本
在某些情况下,您要替换的文本可能包含换行符,即换行符,段落换行符,分节符等。为了应对这种情况,Aspose.Words for C ++在搜索和替换字符串中支持以下元字符。
- &p 换段
- &b 用于分节符
- &m 用于分页符
- &l 手动换行
下面的代码示例演示如何使用Word文档中的元字符查找和替换文本。
// Load MS Word document System::SharedPtrdoc = System::MakeObject(u"Document.doc"); // Create find and replace options System::SharedPtroptions = System::MakeObject(); // Find and replace the text doc->get_Range()->Replace(u"This is Line 1&pThis is Line 2", u"This is replaced line", options); doc->get_Range()->Replace(u"This is Line 1&mThis is Line 2", u"Page break is replaced with new text.", options); // Save the updated document doc->Save(u"updated.doc");
在查找和替换操作过程中忽略文本
Aspose.Words for C ++还允许您在查找和替换操作期间忽略字段和修订中的文本。该FindReplaceOptions类,您可以指定选项来添加这个定制。FindReplaceOptions类提供以下方法来在不同情况下忽略文本:
- set_IgnoreFields(bool) –忽略字段内的文本
- set_IgnoreDeleted(bool) –忽略删除版本内的文本
- set_IgnoreInserted(bool) –忽略插入修订中的文本
下面的代码示例演示如何在上述每种情况下忽略文本。
// Load MS Word document System::SharedPtrdoc = System::MakeObject(u"Document.doc"); // Create find and replace options System::SharedPtroptions = System::MakeObject(); // Replace 'e' in document ignoring text inside field options->set_IgnoreFields(true); doc->get_Range()->Replace(System::MakeObject(u"e"), u"*", options); // Replace 'e' in document ignoring deleted text options->set_IgnoreDeleted(true); doc->get_Range()->Replace(System::MakeObject(u"e"), u"*", options); // Replace 'e' in document ignoring inserted text options->set_IgnoreInserted(true); doc->get_Range()->Replace(System::MakeObject(u"e"), u"*", options); // Save the updated document doc->Save(u"updated.doc");