62,052
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 处理关键字,去除已添加链接的关键字
/// </summary>
/// <param name="src">源字符串</param>
/// <param name="keys">关键字列表</param>
/// <returns>处理后结果</returns>
private List<string> removeRepKey(string src, List<string> keys)
{
Regex reg = new Regex(@"(?is)<a\b[^>]*>(.*?)</a>");
MatchCollection mc = reg.Matches(src);
foreach (Match m in mc)
{
for (int i = keys.Count - 1; i >= 0; i--)
{
if (keys[i].ToLower() == m.Groups[1].Value.ToLower())
{
keys.RemoveAt(i);
}
}
}
return keys;
}
/// <summary>
/// 给关键字加链接,同一关键字只加一次
/// </summary>
/// <param name="src">源字符串</param>
/// <param name="keys">关键字列表</param>
/// <returns>替换后结果</returns>
private string keyAddUrl(string src, List<string> keys)
{
Regex reg = new Regex(@"(?i)(?:^|(?<!<(a|pre)\b[^<>]*)>)[^<>]*(?:<|$)");
int length = 0;
string temp = string.Empty;
return reg.Replace(src, delegate(Match m)
{
temp = m.Value;
length = temp.Length;
for (int i = keys.Count - 1; i >= 0; i--)
{
temp = Regex.Replace(temp, @"(?is)^((?:(?:(?!" + Regex.Escape(keys[i]) + @"|</?a\b).)*<a\b(?:(?!</?a\b).)*</a>)*(?:(?!" + Regex.Escape(keys[i]) + @"|</?a\b).)*)(?<tag>" + Regex.Escape(keys[i]) + @")", @"$1<a href=""http://www.21shipin.com"" target=""_blank"" title=""${tag}"">${tag}</a>");
if (length != temp.Length)
{
keys.Remove(keys[i]);
}
length = temp.Length;
}
return temp;
});
}
//调用
string str1 = "我想加个<pre>c语言教程,c语言</pre>还学习c语言教程,<p title=\"c语言\">c语言</p>,我想看的是C语言视频教程,其它什么<a href=\"http://www.21shipin.com\" target=\"_blank\" title=\"C语言\">C语言</a>教程,我都不想看。我喜欢C语言,还是C语言。";
List<string> keys = new List<string>(new string[] { "c语言", "C语言教程", "c语言视频教程" });
keys = removeRepKey(str1, keys);
string result = keyAddUrl(str1, keys);
richTextBox2.Text = result;
/// <summary>
/// 处理关键字,去除已添加链接的关键字
/// </summary>
/// <param name="src">源字符串</param>
/// <param name="keys">关键字列表</param>
/// <returns>处理后结果</returns>
private List<string> removeRepKey(string src, List<string> keys)
{
Regex reg = new Regex(@"(?is)<a\b[^>]*>(.*?)</a>");
MatchCollection mc = reg.Matches(src);
foreach (Match m in mc)
{
for (int i = keys.Count - 1; i >= 0; i--)
{
if (keys[i].ToLower() == m.Groups[1].Value.ToLower())
{
keys.RemoveAt(i);
}
}
}
return keys;
}
/// <summary>
/// 给关键字加链接,同一关键字只加一次
/// </summary>
/// <param name="src">源字符串</param>
/// <param name="keys">关键字列表</param>
/// <returns>替换后结果</returns>
private string keyAddUrl(string src, List<string> keys)
{
Regex reg = new Regex(@"(?in)[^<>]+(?=(<(?!/(pre|a))[^<>]*>[^<>]*)*(<(a|pre)\b|$))");
int length = 0;
string temp = string.Empty;
return reg.Replace(src, delegate(Match m)
{
temp = m.Value;
length = temp.Length;
for (int i = keys.Count - 1; i >= 0; i--)
{
temp = Regex.Replace(temp, @"(?is)^((?:(?:(?!" + Regex.Escape(keys[i]) + @"|</?a\b).)*<a\b(?:(?!</?a\b).)*</a>)*(?:(?!" + Regex.Escape(keys[i]) + @"|</?a\b).)*)(?<tag>" + Regex.Escape(keys[i]) + @")", @"$1<a href=""http://www.21shipin.com"" target=""_blank"" title=""${tag}"">${tag}</a>");
if (length != temp.Length)
{
keys.Remove(keys[i]);
}
length = temp.Length;
}
return temp;
});
}
//调用
string str1 = "我想加个<pre><b>c语言教程</b>,c语言</pre>还学习c语言教程,<p title=\"c语言\">c语言</p>,我想看的是C语言视频教程,其它什么<a href=\"http://www.21shipin.com\" target=\"_blank\" title=\"C语言\">C语言</a>教程,我都不想看。我喜欢C语言,还是C语言。";
List<string> keys = new List<string>(new string[] { "c语言", "C语言教程", "c语言视频教程" });
keys = removeRepKey(str1, keys);
string result = keyAddUrl(str1, keys);
richTextBox2.Text = result;
/// <summary>
/// 给关键字加链接,同一关键字只加一次
/// </summary>
/// <param name="src">源字符串</param>
/// <param name="keys">关键字泛型</param>
/// <returns>替换后结果</returns>
private string keyAddUrl(string src, List<string> keys)
{
Regex reg = new Regex(@"(?i)(?:^|(?<!<a\b[^<>]*)>)[^<>]*(?:<|$)");
int length = 0;
string temp = string.Empty;
return reg.Replace(src, delegate(Match m)
{
temp = m.Value;
length = temp.Length;
for (int i = keys.Count - 1; i >= 0; i--)
{
temp = Regex.Replace(temp, @"(?is)^((?:(?:(?!" + Regex.Escape(keys[i]) + @"|</?a\b).)*<a\b(?:(?!</?a\b).)*</a>)*(?:(?!" + Regex.Escape(keys[i]) + @"|</?a\b).)*)(?<tag>" + Regex.Escape(keys[i]) + @")", @"$1<a href=""http://www.21shipin.com"" target=""_blank"" title=""${tag}"">${tag}</a>");
if (length != temp.Length)
{
keys.Remove(keys[i]);
}
length = temp.Length;
}
return temp;
});
}
//调用
string str1 = "我想学习c语言教程,我想看的是C语言视频教程,其它什么<a href=\"http://www.21shipin.com\" target=\"_blank\" title=\"C语言\">C语言</a>教程,我都不想看。我喜欢C语言,还是C语言。";
List<string> keys = new List<string>(new string[] { "c语言", "C语言教程", "c语言视频教程" });
string result = keyAddUrl(str1, keys);
richTextBox2.Text = result;
/*---------输出---------
我想学习<a href="http://www.21shipin.com" target="_blank" title="c语言教程">c语言教程</a>,我想看的是<a href="http://www.21shipin.com" target="_blank" title="C语言视频教程">C语言视频教程</a>,其它什么<a href="http://www.21shipin.com" target="_blank" title="C语言">C语言</a>教程,我都不想看。我喜欢<a href="http://www.21shipin.com" target="_blank" title="C语言">C语言</a>,还是C语言。
*/