ค้นหาและแทนที่
คุณสามารถนำทางภายในเอกสารของคุณโดยใช้แป้นพิมพ์และเมาส์แต่ถ้าคุณมีหลายหน้าเ มันจะใช้เวลามากขึ้นเมื่อคุณต้องการแทนที่อักขระหรือคำที่คุณได้ใช้ในเอกสารของคุณ. ฟังก์ชัน"ค้นหาและแทนที่"ช่วยให้คุณสามารถหาลำดับของตัวอักษรในเอกสารและแทนที่ด้วยลำ.
Aspose.Wordsช่วยให้คุณสามารถค้นหาสตริงที่เฉพาะเจาะจงหรือรูปแบบนิพจน์ปกติในเอกสารของคุณและแทนที่ด้วยทางเลือกโดยไม่ต้องติดตั้งและใช้โปรแกรมประยุกต์เพิ่มเติมเช่นMicrosoft Word นี้จะเพิ่มความเร็วในการพิมพ์จำนวนมากและการจัดรูปแบบงานที่อาจช่วยให้คุณประหยัดชั่.
บทความนี้อธิบายถึงวิธีการใช้การแทนที่สตริงและนิพจน์ทั่วไปด้วยการสนับสนุนของอักขระ.
วิธีการค้นหาและแทนที่
Aspose.Wordsมีสองวิธีที่จะใช้การดำเนินการค้นหาและแทนที่โดยใช้ต่อไปนี้:
- Simple string replacement–เมื่อต้องการค้นหาและแทนที่สตริงที่เฉพาะเจาะจงกับอีกสตริงหนึ่งคุณต้องระบุสตริงการค้นหา(อักขระตัวเลขและตัวเลข)ที่จะถูกแทนที่ตามที่เกิดขึ้นทั้งหมดด้วยสตริงการแทนที่ที่ระบุอื่น สตริงทั้งสองต้องไม่มีสัญลักษณ์ คำนึงถึงว่าการเปรียบเทียบสายอักขระอาจเป็นตัวพิมพ์ใหญ่หรือคุณอาจไม่แน่ใจในการสะก.
- Regular expressions-เพื่อระบุนิพจน์ทั่วไปเพื่อค้นหาการจับคู่สตริงที่ตรงกันและแทนที่ตามนิพจน์ปกติของคุณ หมายเหตุว่าคำถูกกำหนดเป็นถูกสร้างขึ้นของอักขระตัวเลขเท่านั้น คำทั้งหมดเท่านั้นที่ถูกจับคู่และสตริงการป้อนข้อมูลที่เกิดขึ้นจะมีสัญลักษณ์แล้ววลีที่จะไม่พบ.
การแทนที่ด้วยสตริงแบบง่ายและนิพจน์ทั่วไปเพื่อระบุตัวแบ่งภายในค้นหาและแทนที่การดำเ.
Aspose.Wordsแสดงฟังก์ชันการค้นหาและแทนที่ด้วยเนมสเปซAspose.Words.Replacing คุณสามารถทำงานกับตัวเลือกมากมายในระหว่างกระบวนการค้นหาและแทนที่โดยใช้ชั้นเรียนFindReplaceOptions.
ค้นหาและแทนที่ข้อความโดยใช้การเปลี่ยนสตริงแบบง่าย
คุณสามารถใช้วิธีการ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); |
คุณสามารถสังเกตเห็นความแตกต่างระหว่างเอกสารก่อนที่จะใช้การเปลี่ยนสตริงอย่างง่:
![before-simple-string-replacement-aspose-words-cpp](before-simple-string-replacement.png)
และหลังจากใช้การเปลี่ยนสตริงง่าย:
![after-simple-string-replacement-aspose-words-cpp](after-simple-string-replacement.png)
ค้นหาและแทนที่ข้อความโดยใช้นิพจน์ทั่วไป
นิพจน์ทั่วไป(รีเอกซ์)เป็นรูปแบบที่อธิบายลำดับของข้อความ สมมติว่าคุณต้องการที่จะแทนที่การเกิดขึ้นคู่ทั้งหมดของคำที่มีการเกิดขึ้นคำเดียว จากนั้นคุณสามารถใช้นิพจน์ปกติต่อไปนี้เพื่อระบุรูปแบบคำสองคำ:([a-zA-Z]+) \1
.
ใช้วิธีการReplaceอื่นๆเพื่อค้นหาและแทนที่ชุดอักขระเฉพาะโดยการตั้งค่าพารามิเตอร์Regex
เป็นรูปแบบ.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแทนที่สตริงที่ตรงกับรูปแบบนิพจน์ปกติกับสตริงแทนที่ที่ระบุ:
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); |
คุณสามารถสังเกตเห็นความแตกต่างระหว่างเอกสารก่อนที่จะใช้การแทนสตริงด้วยนิพจน์:
![before-replacement-with-regular-expressions-aspose-words-cpp](before-replacement-with-regular-expressions.png)
และหลังจากใช้สตริงแทนด้วยนิพจน์ปกติ:
![after-replacement-with-regular-expressions-aspose-words-cpp](after-replacement-with-regular-expressions.png)
ค้นหาและแทนที่สตริงโดยใช้อักขระเมตา
คุณสามารถใช้เมตาอักขระในสตริงการค้นหาหรือสตริงการแทนที่ได้ หากข้อความหรือวลีใดวลีหนึ่งประกอบด้วยหลายย่อหน้า หลายส่วน หรือหลายหน้า เมตาอักขระบางตัวได้แก่ &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); |
ค้นหาและแทนที่สตริงในส่วนหัว/ส่วนท้ายของเอกสาร
คุณสามารถค้นหาและแทนที่ข้อความในส่วนหัว/ส่วนท้ายของเอกสารคำโดยใช้ชั้นเรียนHeaderFooter.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแทนที่ข้อความของส่วนหัวในเอกสารของคุณ:
คุณสามารถสังเกตเห็นความแตกต่างระหว่างเอกสารก่อนที่จะใช้การเปลี่ยนสตริงส่วนหัว:
![before-applying-header-string-replacement-aspose-words-cpp](before-applying-header-string-replacement.png)
และหลังจากใช้เปลี่ยนสตริงส่วนหัว:
![after-applying-header-string-replacement-aspose-words-cpp](after-applying-header-string-replacement.png)
ตัวอย่างรหัสที่จะแทนที่ข้อความของส่วนท้ายในเอกสารของคุณจะคล้ายกับตัวอย่างรหัสส่วน ทั้งหมดที่คุณต้องทำคือการแทนที่ต่อไปนี้สองบรรทัด:
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);
คุณสามารถสังเกตเห็นความแตกต่างระหว่างเอกสารก่อนที่จะใช้สตริงท้ายแทน:
![before-applying-footer-string-replacement-aspose-words-cpp](before-applying-footer-string-replacement.png)
และหลังจากใช้สตริงส่วนท้ายแทน:
![after-applying-footer-string-replacement-aspose-words-cpp](after-applying-footer-string-replacement.png)
ไม่สนใจข้อความระหว่างค้นหาและแทนที่
ในขณะที่ใช้การดำเนินการค้นหาและแทนที่,คุณสามารถละเว้นบางส่วนของข้อความ. ดังนั้นบางส่วนของข้อความที่สามารถแยกออกจากการค้นหาและการค้นหาและแทนที่สามา.
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.
หากคุณต้องการแทนสตริงด้วยแท็ก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"); | |
} |