<table id="tbcontent" class="class1" bordercolor="#C0DE98" border="1">
<tr>
<th>
列头1
</th>
<th>
列头2
</th>
<th>
列头3
</th>
<th>
列头4
</th>
<th>
列头5
</th>
</tr>
<tr>
<td>
值1
</td>
<td>
值1
</td>
<td>
值1
</td>
<td>
值1
</td>
<td>
值1
</td>
</tr>
<tr> <td>
值1
</td>
<td>
值1
</td>
<td>
值1
</td>
<td>
值1
</td>
<td>
值1
</td>
</tr>
</table>现在想求一个正则表达式,上面的<th>是列头,下面的是值,如何把列头取出来,然后把值对应到列里面,呵呵,谢谢了
<tr>
<th>
列头1
</th>
<th>
列头2
</th>
<th>
列头3
</th>
<th>
列头4
</th>
<th>
列头5
</th>
</tr>
<tr>
<td>
值1
</td>
<td>
值1
</td>
<td>
值1
</td>
<td>
值1
</td>
<td>
值1
</td>
</tr>
<tr> <td>
值1
</td>
<td>
值1
</td>
<td>
值1
</td>
<td>
值1
</td>
<td>
值1
</td>
</tr>
</table>现在想求一个正则表达式,上面的<th>是列头,下面的是值,如何把列头取出来,然后把值对应到列里面,呵呵,谢谢了
解决方案 »
- 求助 Cannot access a disposed object.
- 如何查看 C#类里面的 所有函数名?
- 如何用C#按照指定的条件取本地oracle数据库中的数据?
- C#中两个动态链接库的命名空间相同时可否同时使用?
- 有关数组的复制问题
- 请问C#中如何给动态生成的控件设置字体大小
- Visual C#.NET 应用编程150例 随书的代码说是从网站上下载。
- 请教各位高手,在。NET环境下用C#能解决蚁群算法,粒子群算法等问题吗?
- help!!如何合并两dataset 中结构完全一样的表?谢谢100在线等,对了给分
- c#类的属性中可以带参数吗?
- winform:窗体在panel里面打开后,怎样让窗体获得焦点
- 关于socket分包传送!!!!!!!!!!!!!!!没人关注,换个标题
for (int i = 0; i < mc.Count; i++)
{
Response.Write("c1:" + mc[i].Groups["c1"].Value + "c2:" + mc[i].Groups["c2"].Value + "c3:" + mc[i].Groups["c3"].Value + "c4:" + mc[i].Groups["c4"].Value + "c5:" + mc[i].Groups["c5"].Value + "<br>");
}
string pstr = "<th>.*?</th>";
int count = Regex.Matches(str, pstr, RegexOptions.Singleline).Count;2.拼接正则pstr = @"<t(h|d)>(?<c1>[^<]*)</t\1>";
for(int i=2;i<=count;i++)
{
pstr += @"\s*<t\1>(?<c" + i.ToString() + @">[^<]*)</t\1>";
}3.匹配 MatchCollection mc = Regex.Matches(str, pstr, RegexOptions.Singleline);
for (int i = 0; i < mc.Count; i++)
{
for(int i=1;i<=count;i++)
{
Response.Write("c"+i.ToString() + ":" + mc[i].Groups["c"+i.ToString()].Value);
}
}
我没判断是否出现某行不足的情况。 private static void TestRegex26()
{
string html = @"<table id=""tbcontent"" class=""class1"" bordercolor=""#C0DE98"" border=""1"">
<tr>
<th>
列头1
</th>
<th>
列头2
</th>
<th>
列头3
</th>
<th>
列头4
</th>
<th>
列头5
</th>
</tr>
<tr>
<td>
值1
</td>
<td>
值1
</td>
<td>
值1
</td>
<td>
值1
</td>
<td>
值1
</td>
</tr>
<tr> <td>
值1
</td>
<td>
值1
</td>
<td>
值1
</td>
<td>
值1
</td>
<td>
值1
</td>
</tr>
</table>";
Match m = Regex.Match(html, @"(?isn)(\s*<th>\W+(?<column>\w+?)\W+</th>)+\s*?</tr>.+?(?<row><tr>(\s*?<td>\W*(?<item>\w*)\W*?</td>)+\s+?</tr>\s*)+");
List<string> columns = new List<string>();
foreach (Capture c in m.Groups["column"].Captures)
{
columns.Add(c.Value);
}
List<List<string>> rows = new List<List<string>>();
for (int i = 0; i < m.Groups["row"].Captures.Count; i++)
{
List<string> row = new List<string>();
for (int j = i * columns.Count; j < (i + 1) * columns.Count; j++)
{
row.Add(m.Groups["item"].Captures[j].Value);
}
rows.Add(row);
}
foreach (string column in columns)
{
Console.Write(column + "\t");
}
Console.WriteLine();
foreach (List<string> row in rows)
{
foreach (string item in row)
{
Console.Write(item + "\t");
}
Console.WriteLine();
}
}输出列头1 列头2 列头3 列头4 列头5
值1 值1 值1 值1 值1
值1 值1 值1 值1 值1