查找和替换
您可以使用键盘和鼠标轻松地在文档中导航,但如果您有许多页面要滚动,则需要相当长的时间才能在长文档中找到特定文本。 当您想要替换文档中使用的某些字符或单词时,这将更加耗时。 “查找和替换"功能使您能够在文档中查找字符序列,并将其替换为另一个字符序列。
Aspose.Words允许您在文档中查找特定的字符串或正则表达式模式,并将其替换为替代项,而无需安装和使用其他应用程序(如Microsoft Word)。 这将加快许多打字和格式化任务,可能节省您的工作时间。
本文介绍了如何在元字符的支持下应用字符串替换和正则表达式。
查找和替换{#ways-to-find-and-replace}的方法
Aspose.Words提供了两种应用查找和替换操作的方法,方法如下:
- Simple string replacement-要查找并用另一个替换特定字符串,您需要指定一个搜索字符串(字母数字字符),该字符串将根据所有出现的情况用另一个指定的替换字符串替换。 两个字符串都不能包含符号。 请注意,字符串比较可能区分大小写,或者您可能不确定拼写或有几个类似的拼写。
- Regular expressions-指定正则表达式以查找精确的字符串匹配项,并根据正则表达式替换它们。 请注意,单词被定义为仅由字母数字字符组成。 如果只使用匹配的整个单词执行替换,并且输入字符串恰好包含符号,则不会找到短语。
此外,您还可以使用带有简单字符串替换和正则表达式的特殊元字符来指定查找和替换操作中的分隔符。
Aspose.Words使用Aspose.Words.Replacing命名空间提供查找和替换功能。 您可以使用FindReplaceOptions类在查找和替换过程中使用许多选项。
使用简单字符串替换{#find-and-replace-text-using-simple-string-replacement}查找和替换文本
您可以使用Replace方法之一来查找或替换特定字符串,并返回所做的替换数。 在这种情况下,您可以指定要替换的字符串、将替换其所有出现的字符串、替换是否区分大小写以及是否仅影响独立单词。
下面的代码示例演示如何查找字符串”CustomerName“并将其替换为字符串*“James Bond”*:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
// Load a Word Docx document by creating an instance of the Document class. | |
auto doc = System::MakeObject<Document>(); | |
auto builder = System::MakeObject<DocumentBuilder>(doc); | |
builder->Writeln(u"Hello _CustomerName_, "); | |
// Specify the search string and replace string using the Replace method. | |
doc->get_Range()->Replace(u"_CustomerName_", u"James Bond", System::MakeObject<FindReplaceOptions>()); | |
// Save the result. | |
System::String outputPath = outputDataDir + u"Range.ReplaceSimple.docx"; | |
doc->Save(outputPath); |
在应用简单字符串替换之前,您可以注意到文档之间的差异:

和应用简单的字符串替换后:

使用正则表达式{#find-and-replace-text-using-regular-expressions}查找和替换文本
正则表达式(regex)是描述特定文本序列的模式。 假设你想用一个单词的出现替换一个单词的所有双重出现。 然后,您可以应用以下正则表达式来指定双字模式:([a-zA-Z]+) \1
。
通过将Regex
参数设置为正则表达式模式来查找匹配项,使用其他Replace方法来搜索和替换特定的字符组合。
下面的代码示例演示如何将匹配正则表达式模式的字符串替换为指定的替换字符串:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(); | |
auto builder = System::MakeObject<DocumentBuilder>(doc); | |
builder->Writeln(u"sad mad bad"); | |
System::SharedPtr<FindReplaceOptions> options = System::MakeObject<FindReplaceOptions>(); | |
// Replaces all occurrences of the words "sad" or "mad" to "bad". | |
doc->get_Range()->Replace(System::MakeObject<System::Text::RegularExpressions::Regex>(u"[s|m]ad"), u"bad", options); | |
const System::String outputPath = outputDataDir + u"FindAndReplaceWithRegex_out.doc"; | |
doc->Save(outputPath); |
在使用正则表达式应用字符串替换之前,您可以注意到文档之间的差异:

并用正则表达式应用字符串替换后:

使用元字符{#find-and-replace-text-using-metacharacters}查找和替换字符串
如果特定文本或短语由多个段落、部分或页面组成,则可以在搜索字符串或替换字符串中使用元字符。 一些元字符包括**&p**表示段落分隔符,**&b**表示分段分隔符,**&m**表示分页符,**&l**表示换行符。
下面的代码示例演示如何用段落和分页符替换文本:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
builder->get_Font()->set_Name(u"Arial"); | |
builder->Writeln(u"First section"); | |
builder->Writeln(u" 1st paragraph"); | |
builder->Writeln(u" 2nd paragraph"); | |
builder->Writeln(u"{insert-section}"); | |
builder->Writeln(u"Second section"); | |
builder->Writeln(u" 1st paragraph"); | |
System::SharedPtr<FindReplaceOptions> options = System::MakeObject<FindReplaceOptions>(); | |
options->get_ApplyParagraphFormat()->set_Alignment(ParagraphAlignment::Center); | |
// Double each paragraph break after word "section", add kind of underline and make it centered. | |
int32_t count = doc->get_Range()->Replace(u"section&p", u"section&p----------------------&p", options); | |
// Insert section break instead of custom text tag. | |
count = doc->get_Range()->Replace(u"{insert-section}", u"&b", options); | |
System::String savePath = outputDataDir + u"FindReplaceUsingMetaCharacters.ReplaceTextContaingMetaCharacters.docx"; | |
doc->Save(savePath); |
查找并替换文档{#find-and-replace-string-in-header-or-footer-of-a-document}页眉/页脚中的字符串
您可以使用HeaderFooter类在Word文档的页眉/页脚部分中查找和替换文本。
下面的代码示例演示如何替换文档中的标题部分的文本:
在应用标题字符串替换之前,您可以注意到文档之间的差异:

并在应用标题字符串替换后:

替换文档中页脚部分文本的代码示例与之前的页眉代码示例非常相似。 您需要做的就是替换以下两行:
auto header = headersFooters->idx_get(HeaderFooterType::HeaderPrimary);
header->get_Range()->Replace(u"Aspose.Words", u"Remove", options);
与以下:
auto footer = headersFooters->idx_get(HeaderFooterType::FooterPrimary);
footer->get_Range()->Replace(u"(C) 2006 Aspose Pty Ltd.", u"Copyright (C) Aspose Pty Ltd.", options);
在应用页脚字符串替换之前,您可以注意到文档之间的差异:

和应用页脚字符串替换后:

在查找和替换{#ignore-text-during-find-and-replace}期间忽略文本
在应用查找和替换操作时,可以忽略文本的某些段。 因此,文本的某些部分可以从搜索中排除,并且查找和替换可以仅应用于其余部分。
Aspose.Words提供了许多用于忽略文本的查找和替换属性,例如IgnoreDeleted, IgnoreFieldCodes, IgnoreFields, IgnoreFootnotes, 和IgnoreInserted。
下面的代码示例演示如何忽略删除修订版中的文本:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
// Insert non-revised text. | |
builder->Writeln(u"Deleted"); | |
builder->Write(u"Text"); | |
// Remove first paragraph with tracking revisions. | |
doc->StartTrackRevisions(u"John Doe", System::DateTime::get_Now()); | |
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->Remove(); | |
doc->StopTrackRevisions(); | |
System::SharedPtr<FindReplaceOptions> options = System::MakeObject<FindReplaceOptions>(); | |
// Replace 'e' in document while deleted text. | |
options->set_IgnoreDeleted(true); | |
doc->get_Range()->Replace(System::MakeObject<Regex>(u"e"), u"*", options); | |
std::cout << doc->GetText().ToUtf8String() << std::endl; // The output is: Deleted\rT*xt\f | |
// Replace 'e' in document NOT ignoring deleted text. | |
options->set_IgnoreDeleted(false); | |
doc->get_Range()->Replace(System::MakeObject<Regex>(u"e"), u"*", options); | |
std::cout << doc->GetText().ToUtf8String() << std::endl; // The output is: D*l*t*d\rT*xt\f |
自定义查找和替换操作
Aspose.Words提供了许多不同的properties来查找和替换文本,例如使用ApplyFont和ApplyParagraphFormats属性应用特定格式,使用UseSubstitutions属性替换模式中的替换等。
下面的代码示例演示如何突出显示文档中的特定单词:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
// Highlight word "the" with yellow color. | |
auto options = System::MakeObject<FindReplaceOptions>(); | |
options->get_ApplyFont()->set_HighlightColor(System::Drawing::Color::get_Yellow()); | |
// Replace highlighted text. | |
doc->get_Range()->Replace(u"Hello", u"Hello", options); |
Aspose.Words允许您在替换操作期间使用IReplacingCallback接口创建和调用自定义方法。 您可能有一些用例,您需要自定义查找和替换操作,例如使用HTML标记替换使用正则表达式指定的文本,因此基本上您将使用插入HTML来应用replace。
如果需要用HTML标记替换字符串,请应用IReplacingCallback接口自定义查找和替换操作,以便匹配在运行开始时与文档的匹配节点开始。 让我们提供几个使用IReplacingCallback的例子。
下面的代码示例演示如何替换为HTML指定的文本:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
class ReplaceWithHtmlEvaluator : public IReplacingCallback | |
{ | |
typedef ReplaceWithHtmlEvaluator ThisType; | |
typedef IReplacingCallback BaseType; | |
typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo; | |
RTTI_INFO(ThisType, ThisTypeBaseTypesInfo); | |
public: | |
ReplaceWithHtmlEvaluator(System::SharedPtr<FindReplaceOptions> options); | |
ReplaceAction Replacing(System::SharedPtr<ReplacingArgs> args) override; | |
private: | |
System::SharedPtr<FindReplaceOptions> mOptions; | |
}; | |
ReplaceWithHtmlEvaluator::ReplaceWithHtmlEvaluator(System::SharedPtr<FindReplaceOptions> options) | |
{ | |
mOptions = options; | |
} | |
ReplaceAction ReplaceWithHtmlEvaluator::Replacing(System::SharedPtr<ReplacingArgs> args) | |
{ | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(System::DynamicCast<Document>(args->get_MatchNode()->get_Document())); | |
builder->MoveTo(args->get_MatchNode()); | |
// Replace '<CustomerName>' text with a red bold name. | |
builder->InsertHtml(u"<b><font color='red'>James Bond, </font></b>"); | |
args->set_Replacement(u""); | |
return ReplaceAction::Replace; | |
} | |
void ReplaceWithHtml(System::String const& inputDataDir, System::String const& outputDataDir) | |
{ | |
auto doc = System::MakeObject<Document>(); | |
auto builder = System::MakeObject<DocumentBuilder>(doc); | |
builder->Writeln(u"Hello <CustomerName>,"); | |
auto options = System::MakeObject<FindReplaceOptions>(); | |
auto optionsReplacingCallback = System::MakeObject<ReplaceWithHtmlEvaluator>(options); | |
doc->get_Range()->Replace(new Regex(u" <CustomerName>,"), System::String::Empty, options); | |
// Save the modified document. | |
doc->Save(outputDataDir + u"Range.ReplaceWithInsertHtml.doc"); | |
} |
下面的代码示例演示如何突出显示绿色的正数和红色的负数:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
// Replace and Highlight Numbers. | |
class NumberHighlightCallback : public IReplacingCallback | |
{ | |
typedef NumberHighlightCallback ThisType; | |
typedef IReplacingCallback BaseType; | |
typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo; | |
RTTI_INFO(ThisType, ThisTypeBaseTypesInfo); | |
public: | |
NumberHighlightCallback(System::SharedPtr<FindReplaceOptions> const& opt) | |
: mOpt(opt) { } | |
Aspose::Words::Replacing::ReplaceAction Replacing(System::SharedPtr<Aspose::Words::Replacing::ReplacingArgs> args) override | |
{ | |
// Let replacement to be the same text. | |
args->set_Replacement(args->get_Match()->get_Value()); | |
auto val = System::Convert::ToInt32(args->get_Match()->get_Value()); | |
// Apply either red or green color depending on the number value sign. | |
mOpt->get_ApplyFont()->set_Color(val > 0 ? System::Drawing::Color::get_Green() : System::Drawing::Color::get_Red()); | |
return ReplaceAction::Replace; | |
} | |
private: | |
System::SharedPtr<FindReplaceOptions> mOpt; | |
}; |
下面的代码示例演示如何为每行添加行号:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
class LineCounterCallback : public IReplacingCallback | |
{ | |
typedef LineCounterCallback ThisType; | |
typedef IReplacingCallback BaseType; | |
typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo; | |
RTTI_INFO(ThisType, ThisTypeBaseTypesInfo); | |
public: | |
Aspose::Words::Replacing::ReplaceAction Replacing(System::SharedPtr<Aspose::Words::Replacing::ReplacingArgs> args) override | |
{ | |
std::cout << args->get_Match()->get_Value().ToUtf8String() << '\n'; | |
args->set_Replacement(System::String::Format(u"{0} {1}", mCounter++, args->get_Match()->get_Value())); | |
return ReplaceAction::Replace; | |
} | |
private: | |
int32_t mCounter = 1; | |
}; | |
void LineCounter(System::String const& inputDataDir, System::String const& outputDataDir) | |
{ | |
// Create a document. | |
auto doc = System::MakeObject<Document>(); | |
auto builder = System::MakeObject<DocumentBuilder>(doc); | |
// Add lines of text. | |
builder->Writeln(u"This is first line"); | |
builder->Writeln(u"Second line"); | |
builder->Writeln(u"And last line"); | |
// Prepend each line with line number. | |
auto opt = System::MakeObject<FindReplaceOptions>(); | |
opt->set_ReplacingCallback(System::MakeObject<LineCounterCallback>()); | |
doc->get_Range()->Replace(System::MakeObject<Regex>(u"[^&p]*&p"), u"", opt); | |
doc->Save(outputDataDir + u"TestLineCounter.docx"); | |
} |