本帖最后由 hu_86727515 于 2010-09-02 14:37:50 编辑

解决方案 »

  1.   

    http://topic.csdn.net/u/20090604/17/b3da6a6e-f2ab-43ca-95ef-9ac592e6226c.html
      

  2.   


                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());
      

  3.   


    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());
      

  4.   

    我觉得应该修改一下正则表达式
    Regex reg = new Regex(@"(?:^|(?<!<(?:a|pre)\b(?>[^<>]*))>)(?>[^<>]*)(?:<|$)", RegexOptions.IgnoreCase | RegexOptions.Compiled);在第一次替换关键字后,排除<a></a>标签里的内容。我对正则一知半解,还请高手帮忙解决
      

  5.   


    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;
      

  6.   

    protected void Page_Load(object sender, EventArgs e)
        {
            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