日期:2014-05-20  浏览次数:21044 次

给关键词加超链接,但忽略已有的超链接,及所有标签属性的内容,
给关键词加超链接,但忽略已有的超链接,及所有标签属性的内容,

举个例子:

条件:给所有带茶叶的关键词加链接

原字符串内容:

这儿是茶叶的链接。
<img src="茶叶.jpg" width="120" height="120" alt="茶叶" />
<span title="中国好茶叶">中国茶叶</span>
这儿是中国茶叶大观的链接。
这儿是<a href="原有的链接.html">茶叶</a>的现有链接。

要实现的效果:

这儿是<a href="新加的链接.html">茶叶</a>的链接。
<img src="茶叶.jpg" width="120" height="120" alt="茶叶" />
<span title="中国好茶叶">中国<a href="新加的链接.html">茶叶</a></span>
这儿是中国<a href="新加的链接.html">茶叶</a>大观的链接。
这儿是<a href="原有的链接.html">茶叶</a>的现有链接。

求高手给出正则表达式,谢谢。

------解决方案--------------------

        private string ReplaceStr(Match m)
        {
            return "<a href=xxxxxx>" + m.Value + "</a>";
        }       

        private void button10_Click(object sender, EventArgs e)
        {            
            StreamReader reader = new StreamReader("c:\\1.txt");
            string source = reader.ReadToEnd();
            Regex reg = new Regex(@"(?<!((<a[^>].*?)
------解决方案--------------------
(<span[^>].*?)))茶叶");
            source = reg.Replace(source, ReplaceStr);
            MessageBox.Show(source);

        }

------解决方案--------------------
str=Regex.Replace(str,"(?<!\")(茶叶)(?!\"
------解决方案--------------------
</a>)","<a href=\"新加的链接.html\">$1</a>");
------解决方案--------------------
(?<!\"[^>]*)(茶叶)(?!\"
------解决方案--------------------
</a>)

代码中引号要转义