例子:<html>
<head>
</head>
<body>
hello aa
<div>hell</div>
<table>
<tr>
<td>fdfd</td>
</tr>
</table>
<a href="#">ok</a>dd
<a href="#"><img src="" id="s" alt="ok" /></a>
<a href="#"><div id="dd">ok</div></a>
fdfs
...
...
</body>
</html>
我现在需要删掉所有标签(除了<a></a>标签中间的标签的内容)删掉后的结果就是
hello aa
hell
fdfd<a href="#">ok</a>dd
<a href="#"><img src="" id="s" alt="ok" /></a>
<a href="#"><div id="dd">ok</div></a>
fdfs
...
...
期待正则高手来解决,谢谢

解决方案 »

  1.   

    txt=txt.Replace("<html>","").Replace("<head>","").Replace("</head>","").Replace("你想要删除的","")...............Replace字串替换函数就可以解决了
      

  2.   

    using System;
    using System.Text.RegularExpressions;class Program
    {
      static void Main()
      {
        string s = @"<html>
    <head>
    </head>
    <body>
    hello aa
    <div>hell</div>
    <table>
    <tr>
    <td>fdfd</td>
    </tr>
    </table>
    <a href=""#"">ok</a>dd
    <a href=""#""><img src="""" id=""s"" alt=""ok"" /></a>
    <a href=""#""><div id=""dd"">ok</div></a>
    fdfs
    ...
    ...
    </body>
    </html>
    ";
        s = Regex.Replace(s, @"(?si)<a\b.*?</a>|<[^<>]*>", delegate(Match m){return char.ToLower(m.Value[1])=='a' ? m.Value : "";});
        Console.WriteLine(s);
      }
    }
    /* 程序输出:hello aa
    hell
    fdfd
    <a href="#">ok</a>dd
    <a href="#"><img src="" id="s" alt="ok" /></a>
    <a href="#"><div id="dd">ok</div></a>
    fdfs
    ...
    ...*/