ค้นหาและแทนที่
คุณสามารถนำทางภายในเอกสารของคุณได้อย่างง่ายดายโดยใช้แป้นพิมพ์และเมาส์ แต่หากคุณมีหลายหน้าที่ต้องเลื่อนดู อาจต้องใช้เวลาสักครู่เพื่อค้นหาข้อความที่ต้องการในเอกสารขนาดยาว จะใช้เวลานานมากขึ้นเมื่อคุณต้องการแทนที่อักขระหรือคำบางตัวที่คุณใช้ในเอกสารของคุณ ฟังก์ชัน “ค้นหาและแทนที่” ช่วยให้คุณสามารถค้นหาลำดับอักขระในเอกสารและแทนที่ด้วยลำดับอักขระอื่นได้
Aspose.Words ช่วยให้คุณค้นหาสตริงเฉพาะหรือรูปแบบนิพจน์ทั่วไปในเอกสารของคุณและแทนที่ด้วยสตริงอื่นโดยไม่ต้องติดตั้งและใช้แอปพลิเคชันเพิ่มเติม เช่น Microsoft Word ซึ่งจะทำให้งานพิมพ์และการจัดรูปแบบต่างๆ เร็วขึ้น ซึ่งอาจช่วยคุณประหยัดเวลาในการทำงานได้
บทความนี้จะอธิบายวิธีการใช้การแทนที่สตริงและนิพจน์ทั่วไปโดยได้รับการสนับสนุนจากเมตาอักขระ
วิธีค้นหาและแทนที่
Aspose.Words มีสองวิธีในการใช้การค้นหาและแทนที่โดยใช้สิ่งต่อไปนี้:
- การแทนที่สตริงอย่างง่าย – ในการค้นหาและแทนที่สตริงที่ระบุด้วยสตริงอื่น คุณต้องระบุสตริงการค้นหา (อักขระที่เป็นตัวอักษรและตัวเลข) ที่จะถูกแทนที่ด้วยสตริงการแทนที่ที่ระบุอื่นตามเหตุการณ์ทั้งหมด ทั้งสองสายต้องไม่มีสัญลักษณ์ โปรดทราบว่าการเปรียบเทียบสตริงอาจคำนึงถึงตัวพิมพ์เล็กและตัวพิมพ์ใหญ่ หรือคุณอาจไม่แน่ใจในการสะกดหรือมีการสะกดที่คล้ายกันหลายตัว
- นิพจน์ทั่วไป – เพื่อระบุนิพจน์ทั่วไปเพื่อค้นหาสตริงที่ตรงกันทุกประการ และแทนที่ตามนิพจน์ทั่วไปของคุณ โปรดทราบว่าคำที่ถูกกำหนดให้ประกอบด้วยอักขระตัวอักษรและตัวเลขเท่านั้น หากดำเนินการแทนที่โดยจับคู่ทั้งคำเท่านั้นและสตริงอินพุตมีสัญลักษณ์ จะไม่พบวลีใดๆ
นอกจากนี้ คุณยังสามารถใช้อักขระเมตาพิเศษที่มีการแทนที่สตริงอย่างง่ายและนิพจน์ทั่วไปเพื่อระบุตัวแบ่งภายในการดำเนินการค้นหาและแทนที่
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-.NET | |
// Load a Word Docx document by creating an instance of the Document class. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.Writeln("Hello _CustomerName_,"); | |
// Specify the search string and replace string using the Replace method. | |
doc.Range.Replace("_CustomerName_", "James Bond", new FindReplaceOptions()); | |
// Save the result. | |
doc.Save(dataDir + "Range.ReplaceSimple.docx"); |
คุณสามารถสังเกตเห็นความแตกต่างระหว่างเอกสารก่อนที่จะใช้การแทนที่สตริงแบบง่าย:

และหลังจากใช้การแทนที่สตริงแบบง่ายแล้ว:

ค้นหาและแทนที่ข้อความโดยใช้นิพจน์ทั่วไป
นิพจน์ทั่วไป (regex) คือรูปแบบที่อธิบายลำดับข้อความบางลำดับ สมมติว่าคุณต้องการแทนที่คำที่ซ้ำกันทั้งหมดด้วยคำเดียวที่เกิดขึ้น จากนั้นคุณสามารถใช้นิพจน์ทั่วไปต่อไปนี้เพื่อระบุรูปแบบคำคู่: ([a-zA-Z]+) \1
ใช้วิธี Replace อื่นเพื่อค้นหาและแทนที่ชุดอักขระโดยการตั้งค่าพารามิเตอร์ Regex
เป็นรูปแบบนิพจน์ทั่วไปเพื่อค้นหารายการที่ตรงกัน
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแทนที่สตริงที่ตรงกับรูปแบบนิพจน์ทั่วไปด้วยสตริงการแทนที่ที่ระบุ:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.Writeln("sad mad bad"); | |
Assert.AreEqual("sad mad bad", doc.GetText().Trim()); | |
// Replaces all occurrences of the words "sad" or "mad" to "bad". | |
doc.Range.Replace(new Regex("[s|m]ad"), "bad"); | |
// Save the Word document. | |
doc.Save("Range.ReplaceWithRegex.docx"); |
คุณสามารถสังเกตเห็นความแตกต่างระหว่างเอกสารก่อนที่จะใช้การแทนที่สตริงด้วยนิพจน์ทั่วไป:

และหลังจากใช้การแทนที่สตริงด้วยนิพจน์ทั่วไป:

ค้นหาและแทนที่สตริงโดยใช้ MetaCharacters
คุณสามารถใช้อักขระเมตาในสตริงการค้นหาหรือสตริงการแทนที่ได้ หากข้อความหรือวลีใดประกอบด้วยหลายย่อหน้า ส่วน หรือหน้า เมตาอักขระบางตัวประกอบด้วย &พี สำหรับตัวแบ่งย่อหน้า, &ข สำหรับตัวแบ่งส่วน, & ม สำหรับตัวแบ่งหน้า และ &ล สำหรับตัวแบ่งบรรทัด
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแทนที่ข้อความด้วยตัวแบ่งย่อหน้าและหน้า:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.Font.Name = "Arial"; | |
builder.Writeln("First section"); | |
builder.Writeln(" 1st paragraph"); | |
builder.Writeln(" 2nd paragraph"); | |
builder.Writeln("{insert-section}"); | |
builder.Writeln("Second section"); | |
builder.Writeln(" 1st paragraph"); | |
FindReplaceOptions options = new FindReplaceOptions(); | |
options.ApplyParagraphFormat.Alignment = ParagraphAlignment.Center; | |
// Double each paragraph break after word "section", add kind of underline and make it centered. | |
int count = doc.Range.Replace("section&p", "section&p----------------------&p", options); | |
// Insert section break instead of custom text tag. | |
count = doc.Range.Replace("{insert-section}", "&b", options); | |
dataDir = dataDir + "ReplaceTextContaingMetaCharacters_out.docx"; | |
doc.Save(dataDir); |
ค้นหาและแทนที่สตริงในส่วนหัว/ส่วนท้ายของเอกสาร
คุณสามารถค้นหาและแทนที่ข้อความในส่วนหัว/ส่วนท้ายของเอกสาร Word ได้โดยใช้คลาส HeaderFooter
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแทนที่ข้อความของส่วนหัวในเอกสารของคุณ:
คุณสามารถสังเกตเห็นความแตกต่างระหว่างเอกสารก่อนที่จะใช้การแทนที่สตริงส่วนหัว:

และหลังจากใช้การแทนที่สตริงส่วนหัว:

ตัวอย่างโค้ดเพื่อแทนที่ข้อความของส่วนท้ายในเอกสารของคุณคล้ายกับตัวอย่างโค้ดส่วนหัวก่อนหน้ามาก สิ่งที่คุณต้องทำคือแทนที่สองบรรทัดต่อไปนี้:
HeaderFooter header = headersFooters[HeaderFooterType.HeaderPrimary];
header.Range.Replace("Aspose.Words", "Remove", options);
โดยมีดังต่อไปนี้:
HeaderFooter footer = headersFooters[HeaderFooterType.FooterPrimary];
int currentYear = System.DateTime.Now.Year;
footer.Range.Replace("(C) 2006 Aspose Pty Ltd.", $"Copyright (C) {currentYear} by Aspose Pty Ltd.", options);
คุณสามารถสังเกตเห็นความแตกต่างระหว่างเอกสารก่อนที่จะใช้การแทนที่สตริงส่วนท้าย:

และหลังจากใช้การแทนที่สตริงส่วนท้าย:

ละเว้นข้อความระหว่างการค้นหาและแทนที่
ในขณะที่ใช้การดำเนินการค้นหาและแทนที่ คุณสามารถละเว้นบางส่วนของข้อความได้ ดังนั้น บางส่วนของข้อความจึงสามารถแยกออกจากการค้นหาได้ และการค้นหาและการแทนที่สามารถใช้ได้เฉพาะกับส่วนที่เหลือเท่านั้น
Aspose.Words มีคุณสมบัติการค้นหาและแทนที่มากมายสำหรับการละเว้นข้อความ เช่น IgnoreDeleted, IgnoreFieldCodes, IgnoreFields, IgnoreFootnotes และ IgnoreInserted
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการละเว้นข้อความภายในการแก้ไขการลบ:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Insert non-revised text. | |
builder.Writeln("Deleted"); | |
builder.Write("Text"); | |
// Remove first paragraph with tracking revisions. | |
doc.StartTrackRevisions("John Doe", DateTime.Now); | |
doc.FirstSection.Body.FirstParagraph.Remove(); | |
doc.StopTrackRevisions(); | |
Regex regex = new Regex("e"); | |
FindReplaceOptions options = new FindReplaceOptions(); | |
// Replace 'e' in document while ignoring deleted text. | |
options.IgnoreDeleted = true; | |
doc.Range.Replace(regex, "*", options); | |
Assert.AreEqual(doc.GetText().Trim(), "Deleted\rT*xt"); | |
// Replace 'e' in document while not ignoring deleted text. | |
options.IgnoreDeleted = false; | |
doc.Range.Replace(regex, "*", options); |
ปรับแต่งการดำเนินการค้นหาและแทนที่
Aspose.Words มี properties ที่แตกต่างกันมากมายเพื่อค้นหาและแทนที่ข้อความ เช่น การใช้รูปแบบเฉพาะกับคุณสมบัติ ApplyFont และ ApplyParagraphFormats การใช้การทดแทนในรูปแบบการแทนที่ด้วยคุณสมบัติ UseSubstitutions และอื่นๆ
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีเน้นคำเฉพาะในเอกสารของคุณ:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// Highlight word "the" with yellow color. | |
FindReplaceOptions options = new FindReplaceOptions(); | |
options.ApplyFont.HighlightColor = Color.Yellow; | |
// Replace highlighted text. | |
doc.Range.Replace("Hello", "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-.NET | |
public static void ReplaceWithHtml(string dataDir) | |
{ | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.Writeln("Hello <CustomerName>,"); | |
FindReplaceOptions options = new FindReplaceOptions(); | |
options.ReplacingCallback = new ReplaceWithHtmlEvaluator(options); | |
doc.Range.Replace(new Regex(@" <CustomerName>,"), String.Empty, options); | |
// Save the modified document. | |
doc.Save(dataDir + "Range.ReplaceWithInsertHtml.doc"); | |
} | |
private class ReplaceWithHtmlEvaluator : IReplacingCallback | |
{ | |
internal ReplaceWithHtmlEvaluator(FindReplaceOptions options) | |
{ | |
mOptions = options; | |
} | |
//This simplistic method will only work well when the match starts at the beginning of a run. | |
ReplaceAction IReplacingCallback.Replacing(ReplacingArgs args) | |
{ | |
DocumentBuilder builder = new DocumentBuilder((Document)args.MatchNode.Document); | |
builder.MoveTo(args.MatchNode); | |
// Replace '<CustomerName>' text with a red bold name. | |
builder.InsertHtml("<b><font color='red'>James Bond, </font></b>"); args.Replacement = ""; | |
return ReplaceAction.Replace; | |
} | |
private readonly FindReplaceOptions mOptions; | |
} |
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการเน้นตัวเลขบวกด้วยสีเขียวและตัวเลขลบด้วยสีแดง:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// Replace and Highlight Numbers. | |
internal class NumberHighlightCallback : IReplacingCallback | |
{ | |
public NumberHighlightCallback(FindReplaceOptions opt) | |
{ | |
mOpt = opt; | |
} | |
public ReplaceAction Replacing(ReplacingArgs args) | |
{ | |
// Let replacement to be the same text. | |
args.Replacement = args.Match.Value; | |
int val = int.Parse(args.Match.Value); | |
// Apply either red or green color depending on the number value sign. | |
mOpt.ApplyFont.Color = (val > 0) | |
? Color.Green | |
: Color.Red; | |
return ReplaceAction.Replace; | |
} | |
private readonly FindReplaceOptions mOpt; | |
} |
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการเติมหมายเลขบรรทัดไว้ข้างหน้าแต่ละบรรทัด:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
public void LineCounter() | |
{ | |
// Create a document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Add lines of text. | |
builder.Writeln("This is first line"); | |
builder.Writeln("Second line"); | |
builder.Writeln("And last line"); | |
// Prepend each line with line number. | |
FindReplaceOptions opt = new FindReplaceOptions() { ReplacingCallback = new LineCounterCallback() }; | |
doc.Range.Replace(new Regex("[^&p]*&p"), "", opt); | |
doc.Save(@"X:\TestLineCounter.docx"); | |
} | |
internal class LineCounterCallback : IReplacingCallback | |
{ | |
public ReplaceAction Replacing(ReplacingArgs args) | |
{ | |
Debug.WriteLine(args.Match.Value); | |
args.Replacement = string.Format("{0} {1}", mCounter++, args.Match.Value); | |
return ReplaceAction.Replace; | |
} | |
private int mCounter = 1; | |
} |