Aspose.Word —— Find and Replace Overview

Find and Replace Overview
Use Range.Replace to find or replace a particular string within the current range. It returns the number of replacements made, so it is useful for searching strings without replace. An exception is thrown if a captured or replacement string contains one or more special characters: paragraph break, cell break, section break, field start, field separator, field end, inline picture, drawing object and footnote.
The Range.Replace method provides several overloads. Here are the possibilities they provide:
You can specify a string to be replaced, a string that will replace all its occurrences, whether the replacement is case-sensitive, and whether only stand-alone words will be affected. Note that a word is defined as being made up of only alpha-numeric characters. If replace is executed with only whole words only being matched and the input string happens to contain symbols, then no phrases will be found.
You can pass a regular expression pattern used to find matches and a string that will replace them. This overload replaces the whole match captured by the regular expression.
You can pass a regular expression pattern, and an object that implements the IReplacingCallback interface. This sets out a user-defined method, which evaluates replacement at each step, you can also indicate whether the replacement should be done in a forward or backward direction. It is recommended if you are removing nodes during replacement then the replacement should be executed backwards to avoid any potential issues that may arise when removing nodes during the replacement process. A class implementing theIReplacingCallback
interface will define a method IReplacingCallback.Replacing that accepts a ReplacingArgs
object providing data for a custom replace operation. The method should return a ReplaceAction
enumeration value that specifies what happens to the current match during a replace operation - whether it should be replaced, skipped, or the whole replace operation should be terminated.

The following examples show how to use the aforementioned overloads. The sample class provides methods, each of which uses aRange.Replace
overload:
Example 1 simply replaces all occurrences of the word "sad" to "bad".
Example 2 replaces all occurrences of the words "sad" or "mad" to "bad".
Example 3 uses a replace evaluator method to concatenate occurrences of words "sad" or "bad" with the counter value that is incremented each time the new occurrence is found.

Example 1: Replace One Word with Another
Shows how to replace all occurrences of word "sad" to "bad". You can download template file of this example from here.

Example 2: Replace Two Similar Words with One Other
Shows how to replace all occurrences of words "sad" or "mad" to "bad". You can download template file of this example from here.

Example 3: Use a Custom Evaluator
Shows how to replace with a custom evaluator. You can download template file of this example from here.

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET

private class MyReplaceEvaluator : IReplacingCallback

{

/// <summary>

/// This is called during a replace operation each time a match is found.

/// This method appends a number to the match string and returns it as a replacement string.

/// </summary>

ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)

{

e.Replacement = e.Match.ToString() + mMatchNumber.ToString();

mMatchNumber++;

return ReplaceAction.Replace;

}

private int mMatchNumber;

}

view raw Examples-CSharp-Programming-Documents-Find-Replace-ReplaceWithEvaluator-MyReplaceEvaluator.cs hosted with ❤ by GitHub

How to Find and Highlight Text
This section describes how to programmatically find and highlight a word or a phrase in a document using Aspose.Words. It might seem easy to simply find the string of text in a document and change its formatting, but the main difficulty is that due to formatting, the match string could be spread over several runs of text. Consider the following example. The phrase “Hello World!” if formatted and consists of three different runs: Hello is italic, World is bold, and the exclamation mark is regular text:
Hello World!
In addition to formatting, bookmarks in the middle of text will split it into more runs.) The above example is represented in Aspose.Words using the following objects:
Run
(
Run.Text
= “Hello”,
Font.Italic
= true)

Run (Run.Text = “World”,
Font.Bold
= true)

Run (Run.Text = “!”)

This article provides a solution designed to handle the described case – if necessary it collects the word (or phrase) from several runs, while skipping non-run nodes.
The Code
The sample code will open a document and find any instance of the text “your document”. A replace handler is set up to handle the logic to be applied to each resulting match found. In this case the resulting runs are split around the txt and the resulting runs highlighted. Below example finds and highlights all instances of a particular word or a phrase in a Word document. You can download template file of this example from here.

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET

// The path to the documents directory.

string dataDir = RunExamples.GetDataDir_FindAndReplace();

string fileName = "TestFile.doc";

Document doc = new Document(dataDir + fileName);

FindReplaceOptions options = new FindReplaceOptions();

options.ReplacingCallback = new ReplaceEvaluatorFindAndHighlight();

options.Direction = FindReplaceDirection.Backward;

// We want the "your document" phrase to be highlighted.

Regex regex = new Regex("your document", RegexOptions.IgnoreCase);

doc.Range.Replace(regex, "", options);

dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);

// Save the output document.

doc.Save(dataDir);

view raw Examples-CSharp-Programming-Documents-Find-Replace-FindAndHighlight-FindAndHighlight.cs hosted with ❤ by GitHub

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET

private class ReplaceEvaluatorFindAndHighlight : IReplacingCallback

{

/// <summary>

/// This method is called by the Aspose.Words find and replace engine for each match.

/// This method highlights the match string, even if it spans multiple runs.

/// </summary>

ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)

{

// This is a Run node that contains either the beginning or the complete match.

Node currentNode = e.MatchNode;

// The first (and may be the only) run can contain text before the match,

// In this case it is necessary to split the run.

if (e.MatchOffset > 0)

currentNode = SplitRun((Run)currentNode, e.MatchOffset);

// This array is used to store all nodes of the match for further highlighting.

ArrayList runs = new ArrayList();

// Find all runs that contain parts of the match string.

int remainingLength = e.Match.Value.Length;

while (

(remainingLength > 0) &&

(currentNode != null) &&

(currentNode.GetText().Length <= remainingLength))

{

runs.Add(currentNode);

remainingLength = remainingLength - currentNode.GetText().Length;

// Select the next Run node.

// Have to loop because there could be other nodes such as BookmarkStart etc.

do

{

currentNode = currentNode.NextSibling;

}

while ((currentNode != null) && (currentNode.NodeType != NodeType.Run));

}

// Split the last run that contains the match if there is any text left.

if ((currentNode != null) && (remainingLength > 0))

{

SplitRun((Run)currentNode, remainingLength);

runs.Add(currentNode);

}

// Now highlight all runs in the sequence.

foreach (Run run in runs)

run.Font.HighlightColor = Color.Yellow;

// Signal to the replace engine to do nothing because we have already done all what we wanted.

return ReplaceAction.Skip;

}

}

view raw Examples-CSharp-Programming-Documents-Find-Replace-FindAndHighlight-ReplaceEvaluatorFindAndHighlight.cs hosted with ❤ by GitHub

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET

/// <summary>

/// Splits text of the specified run into two runs.

/// Inserts the new run just after the specified run.

/// </summary>

private static Run SplitRun(Run run, int position)

{

Run afterRun = (Run)run.Clone(true);

afterRun.Text = run.Text.Substring(position);

run.Text = run.Text.Substring(0, position);

run.ParentNode.InsertAfter(afterRun, run);

return afterRun;

}

view raw Examples-CSharp-Programming-Documents-Find-Replace-FindAndHighlight-SplitRun.cs hosted with ❤ by GitHub

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,039评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,223评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,916评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,009评论 1 291
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,030评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,011评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,934评论 3 416
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,754评论 0 271
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,202评论 1 309
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,433评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,590评论 1 346
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,321评论 5 342
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,917评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,568评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,738评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,583评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,482评论 2 352

推荐阅读更多精彩内容

  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,451评论 0 13
  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc阅读 2,844评论 0 0
  • 本文转载自知乎 作者:季子乌 笔记版权归笔记作者所有 其中英文语句取自:英语流利说-懂你英语 ——————————...
    Danny_Edward阅读 43,865评论 4 38
  • 2016.12.16凌晨三点, 突然有一个胖子,打开了我的房门。妈妈你别害怕,我是王万里,我给你讲一下,...
    深夜小厨娘阅读 426评论 0 0
  • 今日跟朋友说到回家只想静静地窝在沙发里刷会儿手机看会儿书,听会儿音乐发会儿呆。 什么都计划了唯独没有计划跟...
    小皮酱Sia阅读 248评论 0 2