去除table,tr,td属性的正则表达式
求一个去除table,tr,td属性的正则表达式
先看代码:
我要问的问题是:
用正则表达式匹配.然后变成如下:
如何写这个正则? 谢谢!!
------解决方案--------------------
先看代码:
- HTML code
<table border="0" bgcolor="#0033cc" cellspacing="1" cellpadding="0" width="500" align="center"> <tr bgcolor="#ffffff"> <td align="center" height="18">这里是内容</td> </tr> </table>
我要问的问题是:
用正则表达式匹配.然后变成如下:
- HTML code
<table> <tr> <td>这里是内容</td> </tr> </table>
如何写这个正则? 谢谢!!
------解决方案--------------------
- C# code
using System; using System.Text.RegularExpressions; class Program { static void Main() { string s0 = @" <table border=""0"" bgcolor=""#0033cc"" cellspacing=""1"" cellpadding=""0"" width=""500"" align=""center""> <tr bgcolor=""#ffffff""> <td align=""center"" height=""18""><a href=""."">这里是内容</a></td> </tr> </table>"; string s1 = Regex.Replace(s0, @"(?i)<(table|tr|td)(?:\s+(?:""[^""]*""|'[^']*'|[^""'>])*)?>", "<$1>"); Console.WriteLine(s1); } } /* 程序输出: <table> <tr> <td><a href=".">这里是内容</a></td> </tr> </table> */