HTML字符串操作问题 有点难度的 本帖最后由 SomethingJack 于 2013-12-09 14:08:11 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 给你推荐个东西,你可以看下,学习下,对你这个我觉的有帮助!Jumony Core百度下! 给你推荐个东西,你可以看下,学习下,对你这个我觉的有帮助!Jumony Core百度下!- - 纯字符串操作实现 不实用第三方 上司的要求。 给你推荐个东西,你可以看下,学习下,对你这个我觉的有帮助!Jumony Core百度下!- - 纯字符串操作实现 不实用第三方 上司的要求。你们的上方,好牛B!!!! 可能是style='width:90px' 也可能直接是'width=90px' 这个可以通过两种方式来求值,如果是style那就取style 这是通过width取的就为空,反之一样;var setWidth1=Col标签的宽度值(style=)var setWidth2=Col标签的宽度值(width=) $("#tb1 td").each(function() { $(this).attr("width", setWidth1/2); //给宽度 });table嵌套 怎么套的?? 意思是 使用c#实现。 其实JQ实现比较容易 不过上面要求 谢谢回答, 哥们 对我的需求有何看法 - - 其实转换成XML容易实现的多 有层次了。方便的很不过现在是纯字符串操作。需求说起来比较简单 实现起来有点小难度唉 我写了一半 卡住了 教你一招,用代码先格式化html,这样它每行前面的空格就(层次)固定了,然后再用正则去替换。 string s = File.ReadAllText(@"c:\1.txt", Encoding.GetEncoding("gb2312")); MatchCollection mc = Regex.Matches(s, @"(?is)<table|<col([^/>]+)|<tr|<td|</table"); string str = ""; int grada = 0,n=0; List<object> liobj = new List<object>(); List<string> listr = new List<string>(); foreach (Match m in mc) { if (m.Value == "<table") { grada++; } if (m.Value == "</table") { grada--; } if (m.Value == "<tr") { if (str == "<col") { liobj.Add(listr); listr = new List<string>(); } } if (m.Value == "<td") { if (str != m.Value) { n = 0; } else n++; if (liobj.Count > grada-1 ) { List<string> _temp = liobj[grada-1] as List<string>; if (_temp.Count > n) { int index = s.IndexOf("<td"); if (!string.IsNullOrEmpty(_temp[n])) s = s.Insert(index + 3, " " + _temp[n]); } } s = s.Insert(s.IndexOf("<td") + 1, " "); } if (m.Value.IndexOf("<col") == 0) { listr.Add(m.Groups[1].Value.Trim()); str = "<col"; } else { str = m.Value; } } s = Regex.Replace(s.Replace("< td","<td"),"(?i)<col[^>]*>",""); Console.WriteLine(s);这个就可以实现了。 string s = File.ReadAllText(@"c:\1.txt", Encoding.GetEncoding("gb2312")); List<string> liTable = new List<string>(); for (int i = 0; true; i++) { int index = s.LastIndexOf("<table"); if (index >= 0) { int end = s.IndexOf("</table>", index); if (end > 0) { liTable.Add(s.Substring(index, end - index + 8)); s = s.Remove(index, end - index + 8); s = s.Insert(index, "t_b"+i.ToString()); } else break; } else break; } string toStr = ""; List<string> liWidth = new List<string>(); for (int i = liTable.Count - 1; i >= 0; i--) { string _temp = liTable[i]; string str = ""; MatchCollection mc = Regex.Matches(_temp, @"(?is)<col([^/>]+)|<tr|<td"); int n = 0; foreach (Match m in mc) { if (m.Value == "<td") { if (str != m.Value) { n = 0; } else n++; if (liWidth.Count > n) { int index = _temp.IndexOf("<td"); if (!string.IsNullOrEmpty(liWidth[n])) _temp = _temp.Insert(index + 3, " " + liWidth[n]); } _temp = _temp.Insert(_temp.IndexOf("<td") + 1, " "); } if (m.Value.IndexOf("<col") == 0) { liWidth.Add(m.Groups[1].Value.Trim()); str = "<col"; } else { str = m.Value; } } _temp = Regex.Replace(_temp, "(?is)<col[^>]*>", ""); if (toStr == "") toStr = _temp; else toStr = toStr.Replace("t_b" + i.ToString(), _temp); } Console.WriteLine(toStr);这是第二种办法,一个个table拆分。 十分感谢百忙中抽出时间给我参考代码 但是 你似乎真的想的简单了点。你可能没有测试过我的HTML字符串吧。格式完全错误了。难点1 要考虑嵌套Table 格式可能为<table><col><tr><td><table><col><tr><td></td></tr></table></td></tr></table>难点2 考虑Td有属性colspan.跨列难点3 合并的时候如何正确替换col和 td 取得两标签位置是关键。在同事前辈帮助下 已经完成的七七八八了 还是有问题 还在调试。 CSDN的各位 不妨可以试试看 绝对不是闹眼子的。 这个东西看上去两三句话确实简单 实现起来并非那么容易的 Quote: 引用 15 楼 c02645 的回复:string s = File.ReadAllText(@"c:\1.txt", Encoding.GetEncoding("gb2312")); List<string> liTable = new List<string>(); for (int i = 0; true; i++) { int index = s.LastIndexOf("<table"); if (index >= 0) { int end = s.IndexOf("</table>", index); if (end > 0) { liTable.Add(s.Substring(index, end - index + 8)); s = s.Remove(index, end - index + 8); s = s.Insert(index, "t_b"+i.ToString()); } else break; } else break; } string toStr = ""; List<string> liWidth = new List<string>(); for (int i = liTable.Count - 1; i >= 0; i--) { string _temp = liTable[i]; string str = ""; MatchCollection mc = Regex.Matches(_temp, @"(?is)<col([^/>]+)|<tr|<td"); int n = 0; foreach (Match m in mc) { if (m.Value == "<td") { if (str != m.Value) { n = 0; } else n++; if (liWidth.Count > n) { int index = _temp.IndexOf("<td"); if (!string.IsNullOrEmpty(liWidth[n])) _temp = _temp.Insert(index + 3, " " + liWidth[n]); } _temp = _temp.Insert(_temp.IndexOf("<td") + 1, " "); } if (m.Value.IndexOf("<col") == 0) { liWidth.Add(m.Groups[1].Value.Trim()); str = "<col"; } else { str = m.Value; } } _temp = Regex.Replace(_temp, "(?is)<col[^>]*>", ""); if (toStr == "") toStr = _temp; else toStr = toStr.Replace("t_b" + i.ToString(), _temp); } Console.WriteLine(toStr);这是第二种办法,一个个table拆分。[/quote抱歉 又测试了一下 你这个貌似简单的嵌套可以正确替换 不过我上面给的字符串有点问题 你这个不用删除td的啊,只是把col里的属性拷至对应的td内嘛,然后再删除col节,不是吗? string s = File.ReadAllText(@"c:\1.txt", Encoding.GetEncoding("gb2312")); List<string> liTable = new List<string>(); for (int i = 0; true; i++) { int index = s.LastIndexOf("<table"); if (index >= 0) { int end = s.IndexOf("</table>", index); if (end > 0) { liTable.Add(s.Substring(index, end - index + 8)); s = s.Remove(index, end - index + 8); s = s.Insert(index, "t_b"+i.ToString()); } else break; } else break; } string toStr = ""; List<string> liWidth = new List<string>(); for (int i = liTable.Count - 1; i >= 0; i--) { string _temp = liTable[i]; string str = ""; MatchCollection mc = Regex.Matches(_temp, @"(?is)<col([^/>]+)|<tr|<td"); int n = 0; liWidth = new List<string>(); foreach (Match m in mc) { if (m.Value == "<td") { if (str != m.Value) { n = 0; } else n++; if (liWidth.Count > n) { int index = _temp.IndexOf("<td"); if (!string.IsNullOrEmpty(liWidth[n])) _temp = _temp.Insert(index + 3, " " + liWidth[n]); } _temp = _temp.Insert(_temp.IndexOf("<td") + 1, " "); } if (m.Value.IndexOf("<col") == 0) { liWidth.Add(m.Groups[1].Value.Trim()); str = "<col"; } else { str = m.Value; } } _temp = Regex.Replace(_temp, "(?is)<col[^>]*>", ""); if (toStr == "") toStr = _temp; else toStr = toStr.Replace("t_b" + i.ToString(), _temp); } Console.WriteLine(toStr);我觉得这个完全没有问题 内容是这样<table width='100%' border='1'> <col width='100px'> <col width='200px'> <tr> <td colspan='2'> <table width='100%' border='1'> <col /> <col width='91px' /> <col width='92px' /> <tr> <td align='left'> ISBN </td> <td align='left'> Title </td> <td align='right'> Price </td> <td align='right'> AAA </td> </tr> <tr> <td align='left'> 3476896 </td> <td align='left'> My first HTML </td> <td align='right'> $53 </td> <td align='right'> BB </td> </tr> <tr> <td align='left'> 3476897 </td> <td align='left'> My second HTML </td> <td align='right'> $56 </td> <td align='right'> CC </td> </tr> </table> </td> <td colspan='2'> <table width='100%' border='1'> <col /> <col width='93px' /> <col width='94px' /> <tr> <td align='left'> ISBN </td> <td align='left'> Title </td> <td align='right'> Price </td> <td align='right'> AAA </td> </tr> <tr> <td align='left'> 3476896 </td> <td align='left'> My first HTML </td> <td align='right'> $53 </td> <td align='right'> BB </td> </tr> <tr> <td align='left'> 3476897 </td> <td align='left'> My second HTML </td> <td align='right'> $56 </td> <td align='right'> CC </td> </tr> </table> </td> </tr> </table>出来结果是这样<table width='100%' border='1'> <tr> <td width='100px' colspan='2'> <table width='100%' border='1'> <tr> <td align='left'> ISBN </td> <td width='91px' align='left'> Title </td> <td width='92px' align='right'> Price </td> <td align='right'> AAA </td> </tr> <tr> <td align='left'> 3476896 </td> <td width='91px' align='left'> My first HTML </td> <td width='92px' align='right'> $53 </td> <td align='right'> BB </td> </tr> <tr> <td align='left'> 3476897 </td> <td width='91px' align='left'> My second HTML </td> <td width='92px' align='right'> $56 </td> <td align='right'> CC </td> </tr> </table> </td> <td width='200px' colspan='2'> <table width='100%' border='1'> <tr> <td align='left'> ISBN </td> <td width='93px' align='left'> Title </td> <td width='94px' align='right'> Price </td> <td align='right'> AAA </td> </tr> <tr> <td align='left'> 3476896 </td> <td width='93px' align='left'> My first HTML </td> <td width='94px' align='right'> $53 </td> <td align='right'> BB </td> </tr> <tr> <td align='left'> 3476897 </td> <td width='93px' align='left'> My second HTML </td> <td width='94px' align='right'> $56 </td> <td align='right'> CC </td> </tr> </table> </td> </tr> </table> 前辈 这个HTML只是一个引子,,真正的需求格式太长 我发不了 你关注一下 我可以私信给您看看!。不耽搁您工作的情况下。 怎么把数据库查询的数据集放添加到arraylist中。 工资条导出问题 为什么上传服务器会有这段代码? 谁能帮我转下这一小段vb代码 asp.net 中转向前一页面 asp.net用户控件属性问题 2000 Professsional 可否建多個網站? 为什么“document.Form1.CtrId为空或不是对象”? 看四句话!! #寻求利用存储过程执行搜索的代码 前台绑定后台方法 问一个数据库查询的问题
给你推荐个东西,你可以看下,学习下,对你这个我觉的有帮助!Jumony Core百度下!
给你推荐个东西,你可以看下,学习下,对你这个我觉的有帮助!Jumony Core百度下!
- - 纯字符串操作实现 不实用第三方 上司的要求。
给你推荐个东西,你可以看下,学习下,对你这个我觉的有帮助!Jumony Core百度下!
- - 纯字符串操作实现 不实用第三方 上司的要求。你们的上方,好牛B!!!!
var setWidth2=Col标签的宽度值(width=)
$("#tb1 td").each(function() {
$(this).attr("width", setWidth1/2); //给宽度
});table嵌套 怎么套的??
不过现在是纯字符串操作。需求说起来比较简单 实现起来有点小难度唉 我写了一半 卡住了
MatchCollection mc = Regex.Matches(s, @"(?is)<table|<col([^/>]+)|<tr|<td|</table");
string str = "";
int grada = 0,n=0;
List<object> liobj = new List<object>();
List<string> listr = new List<string>();
foreach (Match m in mc)
{
if (m.Value == "<table")
{
grada++;
}
if (m.Value == "</table")
{
grada--;
}
if (m.Value == "<tr")
{
if (str == "<col")
{
liobj.Add(listr);
listr = new List<string>();
}
}
if (m.Value == "<td")
{
if (str != m.Value)
{
n = 0;
}
else n++;
if (liobj.Count > grada-1 )
{
List<string> _temp = liobj[grada-1] as List<string>;
if (_temp.Count > n)
{
int index = s.IndexOf("<td");
if (!string.IsNullOrEmpty(_temp[n]))
s = s.Insert(index + 3, " " + _temp[n]);
}
}
s = s.Insert(s.IndexOf("<td") + 1, " ");
} if (m.Value.IndexOf("<col") == 0)
{
listr.Add(m.Groups[1].Value.Trim());
str = "<col";
}
else { str = m.Value; } }
s = Regex.Replace(s.Replace("< td","<td"),"(?i)<col[^>]*>","");
Console.WriteLine(s);这个就可以实现了。
List<string> liTable = new List<string>();
for (int i = 0; true; i++)
{
int index = s.LastIndexOf("<table");
if (index >= 0)
{
int end = s.IndexOf("</table>", index);
if (end > 0)
{
liTable.Add(s.Substring(index, end - index + 8));
s = s.Remove(index, end - index + 8);
s = s.Insert(index, "t_b"+i.ToString());
}
else break;
}
else break;
}
string toStr = "";
List<string> liWidth = new List<string>();
for (int i = liTable.Count - 1; i >= 0; i--)
{
string _temp = liTable[i];
string str = "";
MatchCollection mc = Regex.Matches(_temp, @"(?is)<col([^/>]+)|<tr|<td");
int n = 0;
foreach (Match m in mc)
{
if (m.Value == "<td")
{
if (str != m.Value)
{
n = 0;
}
else n++;
if (liWidth.Count > n)
{
int index = _temp.IndexOf("<td");
if (!string.IsNullOrEmpty(liWidth[n]))
_temp = _temp.Insert(index + 3, " " + liWidth[n]);
}
_temp = _temp.Insert(_temp.IndexOf("<td") + 1, " ");
} if (m.Value.IndexOf("<col") == 0)
{
liWidth.Add(m.Groups[1].Value.Trim());
str = "<col";
}
else { str = m.Value; } }
_temp = Regex.Replace(_temp, "(?is)<col[^>]*>", "");
if (toStr == "") toStr = _temp;
else toStr = toStr.Replace("t_b" + i.ToString(), _temp);
}
Console.WriteLine(toStr);
这是第二种办法,一个个table拆分。
十分感谢百忙中抽出时间给我参考代码 但是 你似乎真的想的简单了点。你可能没有测试过我的HTML字符串吧。格式完全错误了。难点1 要考虑嵌套Table 格式可能为<table><col><tr><td><table><col><tr><td></td></tr></table></td></tr></table>
难点2 考虑Td有属性colspan.跨列
难点3 合并的时候如何正确替换col和 td 取得两标签位置是关键。在同事前辈帮助下 已经完成的七七八八了 还是有问题 还在调试。 CSDN的各位 不妨可以试试看 绝对不是闹眼子的。 这个东西看上去两三句话确实简单 实现起来并非那么容易的
string s = File.ReadAllText(@"c:\1.txt", Encoding.GetEncoding("gb2312"));
List<string> liTable = new List<string>();
for (int i = 0; true; i++)
{
int index = s.LastIndexOf("<table");
if (index >= 0)
{
int end = s.IndexOf("</table>", index);
if (end > 0)
{
liTable.Add(s.Substring(index, end - index + 8));
s = s.Remove(index, end - index + 8);
s = s.Insert(index, "t_b"+i.ToString());
}
else break;
}
else break;
}
string toStr = "";
List<string> liWidth = new List<string>();
for (int i = liTable.Count - 1; i >= 0; i--)
{
string _temp = liTable[i];
string str = "";
MatchCollection mc = Regex.Matches(_temp, @"(?is)<col([^/>]+)|<tr|<td");
int n = 0;
liWidth = new List<string>();
foreach (Match m in mc)
{
if (m.Value == "<td")
{
if (str != m.Value)
{
n = 0;
}
else n++;
if (liWidth.Count > n)
{
int index = _temp.IndexOf("<td");
if (!string.IsNullOrEmpty(liWidth[n]))
_temp = _temp.Insert(index + 3, " " + liWidth[n]);
}
_temp = _temp.Insert(_temp.IndexOf("<td") + 1, " ");
} if (m.Value.IndexOf("<col") == 0)
{
liWidth.Add(m.Groups[1].Value.Trim());
str = "<col";
}
else { str = m.Value; } }
_temp = Regex.Replace(_temp, "(?is)<col[^>]*>", "");
if (toStr == "") toStr = _temp;
else toStr = toStr.Replace("t_b" + i.ToString(), _temp);
}
Console.WriteLine(toStr);我觉得这个完全没有问题
<col width='100px'>
<col width='200px'>
<tr>
<td colspan='2'>
<table width='100%' border='1'>
<col />
<col width='91px' />
<col width='92px' />
<tr>
<td align='left'>
ISBN
</td>
<td align='left'>
Title
</td>
<td align='right'>
Price
</td>
<td align='right'>
AAA
</td>
</tr>
<tr>
<td align='left'>
3476896
</td>
<td align='left'>
My first HTML
</td>
<td align='right'>
$53
</td>
<td align='right'>
BB
</td>
</tr>
<tr>
<td align='left'>
3476897
</td>
<td align='left'>
My second HTML
</td>
<td align='right'>
$56
</td>
<td align='right'>
CC
</td>
</tr>
</table>
</td>
<td colspan='2'>
<table width='100%' border='1'>
<col />
<col width='93px' />
<col width='94px' />
<tr>
<td align='left'>
ISBN
</td>
<td align='left'>
Title
</td>
<td align='right'>
Price
</td>
<td align='right'>
AAA
</td>
</tr>
<tr>
<td align='left'>
3476896
</td>
<td align='left'>
My first HTML
</td>
<td align='right'>
$53
</td>
<td align='right'>
BB
</td>
</tr>
<tr>
<td align='left'>
3476897
</td>
<td align='left'>
My second HTML
</td>
<td align='right'>
$56
</td>
<td align='right'>
CC
</td>
</tr>
</table>
</td>
</tr>
</table>
出来结果是这样
<table width='100%' border='1'>
<tr>
<td width='100px' colspan='2'>
<table width='100%' border='1'>
<tr>
<td align='left'>
ISBN
</td>
<td width='91px' align='left'>
Title
</td>
<td width='92px' align='right'>
Price
</td>
<td align='right'>
AAA
</td>
</tr>
<tr>
<td align='left'>
3476896
</td>
<td width='91px' align='left'>
My first HTML
</td>
<td width='92px' align='right'>
$53
</td>
<td align='right'>
BB
</td>
</tr>
<tr>
<td align='left'>
3476897
</td>
<td width='91px' align='left'>
My second HTML
</td>
<td width='92px' align='right'>
$56
</td>
<td align='right'>
CC
</td>
</tr>
</table>
</td>
<td width='200px' colspan='2'>
<table width='100%' border='1'>
<tr>
<td align='left'>
ISBN
</td>
<td width='93px' align='left'>
Title
</td>
<td width='94px' align='right'>
Price
</td>
<td align='right'>
AAA
</td>
</tr>
<tr>
<td align='left'>
3476896
</td>
<td width='93px' align='left'>
My first HTML
</td>
<td width='94px' align='right'>
$53
</td>
<td align='right'>
BB
</td>
</tr>
<tr>
<td align='left'>
3476897
</td>
<td width='93px' align='left'>
My second HTML
</td>
<td width='94px' align='right'>
$56
</td>
<td align='right'>
CC
</td>
</tr>
</table>
</td>
</tr>
</table>
前辈 这个HTML只是一个引子,,真正的需求格式太长 我发不了 你关注一下 我可以私信给您看看!。不耽搁您工作的情况下。