问题1:
<table border="2"> 转换成: <table>
问题2:
<td colspan="2" align="center"> 转换成:<td colspan="2">
问题3:
<col width=72> 转换成:空三个问题一个问题30分。 其他10分给顶贴的人。
问题解决。就给分。
<table border="2"> 转换成: <table>
问题2:
<td colspan="2" align="center"> 转换成:<td colspan="2">
问题3:
<col width=72> 转换成:空三个问题一个问题30分。 其他10分给顶贴的人。
问题解决。就给分。
Regex regex = new Regex( "\<table.*?\>", RegexOptions.IgnoreCase );
regex.Replace( str, "<table>" );
==>
Regex regex = new Regex( @"\<table.*?\>", RegexOptions.IgnoreCase );
Match m = Regex.Match(str, "<\\s*table\\s+(border=\"\\d*\")[\\s\\S]*>");
str = str.Replace(m.Groups[1].Value, "");
输出:<table align="center">
如果想去掉所有的属性可以这样:
string str = "<table border=\"2\" align=\"center\">";
Match m = Regex.Match(str, "<\\s*table\\s+(border=\"\\d*\"[\\s\\S]*)>");
str = str.Replace(m.Groups[1].Value, "");输出:<table >
怎么能够排除colspan="2" 其中2可能是别的数字。
Match m = Regex.Match(str, "<\\s*td[\\s\\S]*(align=\"\\w*\")[\\s\\S]*>");
str = str.Replace(m.Groups[1].Value, "");输出:<td colspan="2" >
->>
Match m = Regex.Match(str, "<\\s*table[\\s\\S]*(border=\"\\d*\")[\\s\\S]*>");Match m = Regex.Match(str, "<\\s*table\\s+(border=\"\\d*\"[\\s\\S]*)>");
->>
Match m = Regex.Match(str, "<\\s*table[\\s\\S]*(border=\"\\d*\"[\\s\\S]*)>");
但是第二个问题还是有点问题。
liujia_0421(SnowLover) 给出的方法是排除align="center"因为TD的参数比较多,不可能一个一个的排除。有没有方式仅保留colspan或rowspan呢?
a=Regex.Replace(a,@"(?<=<table)\s+?.+?(?=>)","",RegexOptions.IgnoreCase);
//a=Regex.Replace(a,@"<table[^<]+?>","<table>",RegexOptions.IgnoreCase);
a=@"<td colspan=""2"" align=""center""><td colspan=""2"" align=""center"">";
a=Regex.Replace(a,@"(?<=<td\s+?\S+?)\s+?.+?(?=>)","",RegexOptions.IgnoreCase);
//a=Regex.Replace(a,@"(?<=<td\s+?colspan=\S+?)\s+?.+?(?=>)","",RegexOptions.IgnoreCase);
a=@"<col width=72><col width=72>";
a=Regex.Replace(a,@"<col[^<]+?>","",RegexOptions.IgnoreCase);
Match m = Regex.Match(str, "<\\s*col[\\s\\S]*(width=\\d*)[\\s\\S]*>");
str = str.Replace(m.Groups[0].Value, "");输出空.
string str = "<td colspan=\"2\" align=\"center\">";
Match m = Regex.Match(str, "<\\s*td\\s*([\\S]*)(colspan=\"\\d*\")([\\s\\S]*)>");
str = m.Groups[1].Value == "" ? str : str.Replace(m.Groups[1].Value, "");
str = m.Groups[3].Value == "" ? str : str.Replace(m.Groups[3].Value, "");输出:<td colspan="2">