解决方案 »
- 如何刷新
- 我该怎么把<form name="form1"><input type="text" name="time_spent"...</form>的值传到CS里面去啊?
- 關于設置頁面工具栏等的問題
- 如何点击一个复选框后将页面上的全部复选框都选中?
- 建表
- 找一个ASP.net高手开发一个电子商务网站
- 肯请高手指点vb写的asp.net网页调用com+问题,急,在线等
- fpspead下拉列表框联动 高手请进
- 急,急,急,在线等待?DataGrid 数据显示的问题?
- 怎么样才能把treeview系统默认的+/-的图标换成我自己做的图标。
- 从Excel导入数据到GridView碰到的问题?
- 会员登录界面增加模块
string[] ss = { "abc", "acc" };
string s = "abcaccabcaacc";
StringBuilder sb = new StringBuilder();
foreach (var k in ss)
{
int i = s.IndexOf(k);
if (i > -1)
{
sb.Append("[");
sb.Append(k);
sb.Append("]");
s = s.Remove(0, k.Length);
}
}
sb.Append(s);
Console.Write(sb.ToString());
string[] ss = { "冀中能源井矿集团", "冀中能源", "井矿集团" };
string s = "冀中能源井矿集团aa冀中能源bb冀中能源井矿集团cc井矿集团,冀中能源井矿集团cc井矿集团";
StringBuilder sb = new StringBuilder();
foreach (var k in ss)
{
int i = s.IndexOf(k);
if (i > -1)
{
if (i > 0)
{
for (int j = 0; j < i; j++)
{
sb.Append(s[0]);
s = s.Remove(0, 1);
}
}
sb.Append("[");
sb.Append(k);
sb.Append("]");
s = s.Remove(0, k.Length);
}
}
sb.Append(s);
Console.Write(sb.ToString());
Regex reg = new Regex(@"(?:^|(?<!<(?:a|pre)\b(?>[^<>]*))>)(?>[^<>]*)(?:<|$)", RegexOptions.IgnoreCase | RegexOptions.Compiled);在第一次替换关键字后,排除<a></a>标签里的内容。我对正则一知半解,还请高手帮忙解决
List<string> tags = new List<string>(new string[]{ "关键字1", "关键字2", "关键字3", "关键字4", "关键字5" });
int index = -1;
string temp = string.Empty;
List<string> list = new List<string>();
private string RegReplace(Match m)
{
temp = m.Value;
foreach (string tag in tags)
{
index = temp.IndexOf(tag);
if (index > -1)
{
list.Add(tag);
temp = temp.Substring(0, index) + "<a href='/tag/" + tag + ".htm'>" + tag + "</a>" + temp.Substring(index + tag.Length);
}
}
foreach (string s in list)
{
tags.Remove(s);
}
list.Clear();
return temp;
}
//调用
Regex reg = new Regex(@"(?:^|(?<!<(?:a|pre)\b(?>[^<>]*))>)(?>[^<>]*)(?:<|$)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
string result = reg.Replace(yourStr, RegReplace);
richTextBox2.Text = result;
{
string str1 = "冀中能源井矿集团aa冀中能源bb冀中能源井矿集团cc井矿集团,冀中能源井矿集团cc井矿集团";
List<string> keys = new List<string>(new string[] { "冀中能源", "井矿集团", "冀中能源井矿集团" });
keys = removeRepKey(str1, keys);
string result = keyAddUrl(str1, keys);
Response.Write(result);
}
private string RegReplace(Match m)
{
List<string> tags = new List<string>(new string[] { });
int index = -1;
string temp = string.Empty;
List<string> list = new List<string>();
temp = m.Value;
foreach (string tag in tags)
{
index = temp.IndexOf(tag);
if (index > -1)
{
list.Add(tag);
temp = temp.Substring(0, index) + "<a href='www.jznyjkjt.com'>" + tag + "</a>" + temp.Substring(index + tag.Length);
}
}
foreach (string s in list)
{
tags.Remove(s);
}
list.Clear();
return temp;
}
/// <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;
});
}
http://topic.csdn.net/u/20100508/17/8a126e02-b8d6-41dd-9489-24e98e07d201.html