ReplacingArgs
محتويات
[
يخفي
]ReplacingArgs class
يوفر بيانات لعملية استبدال مخصصة.
لمعرفة المزيد، قم بزيارةالبحث والاستبدال مقالة توثيقية.
public class ReplacingArgs
الخصائص
اسم | وصف |
---|---|
GroupIndex { get; set; } | يحدد، حسب الفهرس، المجموعة الملتقطة فيMatch الذي سيتم استبداله بـReplacement سلسلة. |
GroupName { get; set; } | يحدد، بالاسم، المجموعة التي تم التقاطها فيMatch الذي سيتم استبداله بـReplacement سلسلة. |
Match { get; } | الMatch الناتجة عن تطابق تعبير Regular واحد أثناء aيستبدل . |
MatchNode { get; } | الحصول على العقدة التي تحتوي على بداية المباراة. |
MatchOffset { get; } | الحصول على موضع البداية الصفري للمطابقة من بداية العقدة التي تحتوي على بداية المباراة. |
Replacement { get; set; } | الحصول على سلسلة الاستبدال أو تعيينها. |
أمثلة
يوضح كيفية استبدال كافة تكرارات نمط التعبير العادي بسلسلة أخرى، مع تتبع كل هذه الاستبدالات.
public void ReplaceWithCallback()
{
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Our new location in New York City is opening tomorrow. " +
"Hope to see all our NYC-based customers at the opening!");
// يمكننا استخدام كائن "FindReplaceOptions" لتعديل عملية البحث والاستبدال.
FindReplaceOptions options = new FindReplaceOptions();
// قم بتعيين رد اتصال يتتبع أي بدائل ستجريها طريقة "الاستبدال".
TextFindAndReplacementLogger logger = new TextFindAndReplacementLogger();
options.ReplacingCallback = logger;
doc.Range.Replace(new Regex("New York City|NYC"), "Washington", options);
Assert.AreEqual("Our new location in (Old value:\"New York City\") Washington is opening tomorrow. " +
"Hope to see all our (Old value:\"NYC\") Washington-based customers at the opening!", doc.GetText().Trim());
Assert.AreEqual("\"New York City\" converted to \"Washington\" 20 characters into a Run node.\r\n" +
"\"NYC\" converted to \"Washington\" 42 characters into a Run node.", logger.GetLog().Trim());
}
/// <summary>
/// يحتفظ بسجل لكل استبدال نص يتم إجراؤه بواسطة عملية البحث والاستبدال
/// ويلاحظ قيمة النص المطابق الأصلي.
/// </summary>
private class TextFindAndReplacementLogger : IReplacingCallback
{
ReplaceAction IReplacingCallback.Replacing(ReplacingArgs args)
{
mLog.AppendLine($"\"{args.Match.Value}\" converted to \"{args.Replacement}\" " +
$"{args.MatchOffset} characters into a {args.MatchNode.NodeType} node.");
args.Replacement = $"(Old value:\"{args.Match.Value}\") {args.Replacement}";
return ReplaceAction.Replace;
}
public string GetLog()
{
return mLog.ToString();
}
private readonly StringBuilder mLog = new StringBuilder();
}
يوضح كيفية إدراج محتويات المستند بالكامل كبديل لمطابقة في عملية البحث والاستبدال.
public void InsertDocumentAtReplace()
{
Document mainDoc = new Document(MyDir + "Document insertion destination.docx");
// يمكننا استخدام كائن "FindReplaceOptions" لتعديل عملية البحث والاستبدال.
FindReplaceOptions options = new FindReplaceOptions();
options.ReplacingCallback = new InsertDocumentAtReplaceHandler();
mainDoc.Range.Replace(new Regex("\\[MY_DOCUMENT\\]"), "", options);
mainDoc.Save(ArtifactsDir + "InsertDocument.InsertDocumentAtReplace.docx");
}
private class InsertDocumentAtReplaceHandler : IReplacingCallback
{
ReplaceAction IReplacingCallback.Replacing(ReplacingArgs args)
{
Document subDoc = new Document(MyDir + "Document.docx");
// أدخل مستندًا بعد الفقرة التي تحتوي على النص المطابق.
Paragraph para = (Paragraph)args.MatchNode.ParentNode;
InsertDocument(para, subDoc);
// قم بإزالة الفقرة التي تحتوي على النص المطابق.
para.Remove();
return ReplaceAction.Skip;
}
}
/// <summary>
/// إدراج كافة العقد في مستند آخر بعد فقرة أو جدول.
/// </summary>
private static void InsertDocument(Node insertionDestination, Document docToInsert)
{
if (insertionDestination.NodeType == NodeType.Paragraph || insertionDestination.NodeType == NodeType.Table)
{
CompositeNode dstStory = insertionDestination.ParentNode;
NodeImporter importer =
new NodeImporter(docToInsert, insertionDestination.Document, ImportFormatMode.KeepSourceFormatting);
foreach (Section srcSection in docToInsert.Sections.OfType<Section>())
foreach (Node srcNode in srcSection.Body)
{
// تخطي العقدة إذا كانت آخر فقرة فارغة في القسم.
if (srcNode.NodeType == NodeType.Paragraph)
{
Paragraph para = (Paragraph)srcNode;
if (para.IsEndOfSection && !para.HasChildNodes)
continue;
}
Node newNode = importer.ImportNode(srcNode, true);
dstStory.InsertAfter(newNode, insertionDestination);
insertionDestination = newNode;
}
}
else
{
throw new ArgumentException("The destination node must be either a paragraph or table.");
}
}
أنظر أيضا
- interface IReplacingCallback
- class Range
- method Replace
- مساحة الاسم Aspose.Words.Replacing
- المجسم Aspose.Words