发布时间:2020-05-02  栏目:数据  评论:0 Comments

return new sqlstring(m.tostring()); }首页 1 2 末页

options.ReplacingCallback = new ReplaceEvaluatorFindAndHighlight();

// 替换字符串中与指定的匹配模式一致的字符串
[sqlfunction(isdeterministic = true, isprecise = true)] public static
sqlstring regexreplace(sqlstring expression, sqlstring pattern,
sqlstring replacement) { return new
sqlstring(regex.replace(expression.tostring(), pattern.tostring(),
replacement.tostring())); }


match m = regex.match(expression.tostring().substring((int) position –
1),pattern.tostring()); for (int i = 1; i (int)occurrence; i++) { m =
m.nextmatch(); if (!m.success) return new sqlstring(); }

private int mMatchNumber;

// 提取字符串中与指定的匹配模式一致的字符串
[sqlfunction(isdeterministic = true, isprecise = true)] public static
sqlstring regexsubstring(sqlstring expression, sqlstring pattern,
sqlint32 position, sqlint32 occurrence) { if
(expression.tostring().length position) return new sqlstring(); if
(position = 0) position = 1; if (occurrence = 0) occurrence = 1;


t-sql 正则表达式创建程序集 clrregexclass.cs,并使用
c:windowsmicrosoft.netframeworkv2.0.50727csc.exe /target:library
clrregexclass.cs 编译为 clrregexclass.dll 文件。using system;using
system.data;using system.data.sqlclient;using system.data.sqltypes;using
microsoft.sqlserver.server;using system.text.regularexpressions;

hosted with ❤ by GitHub

public partial class regexp{//
[sqlfunction(isdeterministic = true, isprecise = true)] public static
sqlboolean regexismatch(sqlstring expression, sqlstring pattern) {
return new sqlboolean(regex.ismatch(expression.tostring(),
pattern.tostring())); }


options.Direction = FindReplaceDirection.Backward;

(remainingLength > 0) &&

ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)

// Now highlight all runs in the sequence.

Node currentNode = e.MatchNode;


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

return ReplaceAction.Skip;



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

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

doc.Range.Replace(regex, “”, options);

if (e.MatchOffset > 0)

/// </summary>

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

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

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

currentNode = currentNode.NextSibling;

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

private class MyReplaceEvaluator : IReplacingCallback

Run (Run.Text = “!”)


run.ParentNode.InsertAfter(afterRun, run);

/// <summary>

ArrayList runs = new ArrayList();

hosted with ❤ by GitHub

Document doc = new Document(dataDir + fileName);

// For complete examples and data files, please go to


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

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

/// <summary>

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

/// <summary>


private class ReplaceEvaluatorFindAndHighlight : IReplacingCallback

string fileName = “TestFile.doc”;


run.Font.HighlightColor = Color.Yellow;

while (

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:
= “Hello”,
= true)

// For complete examples and data files, please go to

FindReplaceOptions options = new FindReplaceOptions();

/// </summary>