您的解决方案应该能够做两件主要的事情:1)提取匹配项,即关键字/短语加上附加的左右上下文,以及2)用标记包装搜索词。
提取正则表达式(例如,左右各10个字符)是
(?si)(?<!\S).{0,10}(?<!\S)\S*dog\S*(?!\S).{0,10}(?!\S)
看到了吗
regex demo
细节
-
(?si)
-启用
Singleline
和
IgnoreCase
.
将匹配所有字符,并且模式不区分大小写)
-
(?<!\S)
-
.{0,10}
-
(?<\秒)
-左边的空白边界
-
\S*dog\S*
-
dog
注意
:如果
searchEntireWord
假
\S*
从这个图案部分)
-
(?!\S)
-右边的空白边界
-
.{0,10}
-0到10个字符
-
(?!\秒)
-右边的空白边界。
var expression = string.Format(@"(?si)(?<!\S).{{0,{0}}}(?<!\S)\S*{1}\S*(?!\S).{{0,{0}}}(?!\S)", sizeOfResult, Regex.Escape(searchTerm));
if (searchEntireWord) {
expression = string.Format(@"(?si)(?<!\S).{{0,{0}}}(?<!\S){1}(?!\S).{{0,{0}}}(?!\S)", sizeOfResult, Regex.Escape(searchTerm));
}
{{
{
和
}}
是文字
}
在格式化字符串中。
用强标记包装关键术语的第二个正则表达式要简单得多:
Regex.Replace(x.Value,
searchEntireWord ?
string.Format(@"(?i)(?<!\S){0}(?!\S)", Regex.Escape(searchTerm)) :
string.Format(@"(?i){0}", Regex.Escape(searchTerm)),
"<strong>$&</strong>")
请注意
$&
C代码:
public static List<string> ExtractTexts(string text, string searchTerm, int sizeOfResult, bool searchEntireWord)
{
var expression = string.Format(@"(?si)(?<!\S).{{0,{0}}}(?<!\S)\S*{1}\S*(?!\S).{{0,{0}}}(?!\S)", sizeOfResult, Regex.Escape(searchTerm));
if (searchEntireWord) {
expression = string.Format(@"(?si)(?<!\S).{{0,{0}}}(?<!\S){1}(?!\S).{{0,{0}}}(?!\S)", sizeOfResult, Regex.Escape(searchTerm));
}
return Regex.Matches(text, expression)
.Cast<Match>()
.Select(x => Regex.Replace(x.Value,
searchEntireWord ?
string.Format(@"(?i)(?<!\S){0}(?!\S)", Regex.Escape(searchTerm)) :
string.Format(@"(?i){0}", Regex.Escape(searchTerm)),
"<strong>$&</strong>"))
.ToList();
}
Sample usage (see demo)
var text = "The Dog is a real-pet animal. There's an undogging dog that only undogs non-dogs. It is one of the most obedient animals. There are many kinds of dogs in the world. Some of the are very friendly while some of them a dangerous. Dogs are of different color like black, red, white and brown. Some old them have slippery shiny skin and some have rough skin. Dogs are carnivorous animals. They like eating meat. They have four legs, two ears and a tail. Dogs are trained to perform different tasks. They protect us from thieves b) guarding our house. They are loving animals. A dog is called man's best friend. They are used by the police to find hidden things. They are one of the most useful animals in the world. Doggonit!";
var searchTerm = "dog";
var searchEntireWord = false;
Console.WriteLine("======= 10 ========");
var results = ExtractTexts(text, searchTerm, 10, searchEntireWord);
foreach (var result in results)
Console.WriteLine(result);
输出:
======= 10 ========
(?si)(?<!\S).{0,10}(?<!\S)\S*dog\S*(?!\S).{0,10}(?!\S)
The <strong>Dog</strong> is a
an un<strong>dog</strong>ging <strong>dog</strong> that
only un<strong>dog</strong>s non-<strong>dog</strong>s.
kinds of <strong>dog</strong>s in the
<strong>Dog</strong>s are of
skin. <strong>Dog</strong>s are
a tail. <strong>Dog</strong>s are
A <strong>dog</strong> is called
world. <strong>Dog</strong>gonit!
另一个例子:
Console.WriteLine("======= 15 ========");
results = ExtractTexts(text, searchTerm, 15, searchEntireWord);
foreach (var result in results)
Console.WriteLine(result);
输出:
======= 15 ========
(?si)(?<!\S).{0,15}(?<!\S)\S*dog\S*(?!\S).{0,15}(?!\S)
The <strong>Dog</strong> is a real-pet
There's an un<strong>dog</strong>ging <strong>dog</strong> that only
un<strong>dog</strong>s non-<strong>dog</strong>s. It is one of
many kinds of <strong>dog</strong>s in the world.
a dangerous. <strong>Dog</strong>s are of
rough skin. <strong>Dog</strong>s are
and a tail. <strong>Dog</strong>s are trained to
animals. A <strong>dog</strong> is called
in the world. <strong>Dog</strong>gonit!