<tr bgColor=#8DD3F6>
<td width="134" height="28" align="center" valign="middle" bgcolor="#FFCC00"><strong>名称</strong></td>
<td width="142" align="center" valign="middle" bgcolor="#FFCC00"><strong>地址</strong></td>
<td width="118" align="center" valign="middle" bgcolor="#FFCC00" ><strong>时间</strong></td>
<td width='70' align="center" valign="middle" bgcolor="#FFCC00"><strong>线路</strong></td>
<td width='217' align="center" bgcolor="#FFCC00"><strong>版本信息</strong></td>
<td width="65" align="center" bgcolor="#FFCC00"><strong>星级</strong></td>
<td width="35" align="center" bgcolor="#FFCC00"><strong>介绍</strong></td>
</tr>匹配这样的正则表达式怎么写呢?求教高手!
<td width="134" height="28" align="center" valign="middle" bgcolor="#FFCC00"><strong>名称</strong></td>
<td width="142" align="center" valign="middle" bgcolor="#FFCC00"><strong>地址</strong></td>
<td width="118" align="center" valign="middle" bgcolor="#FFCC00" ><strong>时间</strong></td>
<td width='70' align="center" valign="middle" bgcolor="#FFCC00"><strong>线路</strong></td>
<td width='217' align="center" bgcolor="#FFCC00"><strong>版本信息</strong></td>
<td width="65" align="center" bgcolor="#FFCC00"><strong>星级</strong></td>
<td width="35" align="center" bgcolor="#FFCC00"><strong>介绍</strong></td>
</tr>匹配这样的正则表达式怎么写呢?求教高手!
<td width=""134"" height=""28"" align=""center"" valign=""middle"" bgcolor=""#FFCC00""><strong>名称</strong></td>
<td width=""142"" align=""center"" valign=""middle"" bgcolor=""#FFCC00""><strong>地址</strong></td>
<td width=""118"" align=""center"" valign=""middle"" bgcolor=""#FFCC00"" ><strong>时间</strong></td>
<td width='70' align=""center"" valign=""middle"" bgcolor=""#FFCC00""><strong>线路</strong></td>
<td width='217' align=""center"" bgcolor=""#FFCC00""><strong>版本信息</strong></td>
<td width=""65"" align=""center"" bgcolor=""#FFCC00""><strong>星级</strong></td>
<td width=""35"" align=""center"" bgcolor=""#FFCC00""><strong>介绍</strong></td>
</tr>
"; str = System.Text.RegularExpressions.Regex.Replace(str,@"<([^>\s]*)[^>]*?>", "<$1>");
System.Xml.XmlDocument dom = new System.Xml.XmlDocument();
dom.LoadXml(str);
System.Xml.XmlNodeList nl = dom.SelectNodes("//tr");
foreach (System.Xml.XmlNode node in nl)
{
for(int j=0;j<node.ChildNodes.Count;j++)
{
Response.Write(node.ChildNodes[j].InnerText);
} Response.Write("<BR>");
}
<td width="134" height="28" align="center" valign="middle" bgcolor="#FFCC00"><strong>名称</strong></td>
<td width="142" align="center" valign="middle" bgcolor="#FFCC00"><strong>地址</strong></td>
<td width="118" align="center" valign="middle" bgcolor="#FFCC00" ><strong>时间</strong></td>
<td width='70' align="center" valign="middle" bgcolor="#FFCC00"><strong>线路</strong></td>
<td width='217' align="center" bgcolor="#FFCC00"><strong>版本信息</strong></td>
<td width="65" align="center" bgcolor="#FFCC00"><strong>星级</strong></td>
<td width="35" align="center" bgcolor="#FFCC00"><strong>介绍</strong></td>
</tr><tr bgColor=#8DD3F6>
<td width="134" height="28" align="center" valign="middle" bgcolor="#FFCC00"><strong>名称</strong></td>
<td width="142" align="center" valign="middle" bgcolor="#FFCC00"><strong>地址</strong></td>
<td width="118" align="center" valign="middle" bgcolor="#FFCC00" ><strong>时间</strong></td>
<td width='70' align="center" valign="middle" bgcolor="#FFCC00"><strong>线路</strong></td>
<td width='217' align="center" bgcolor="#FFCC00"><strong>版本信息</strong></td>
<td width="65" align="center" bgcolor="#FFCC00"><strong>星级</strong></td>
<td width="35" align="center" bgcolor="#FFCC00"><strong>介绍</strong></td>
</tr><tr bgColor=#8DD3F6>
<td width="134" height="28" align="center" valign="middle" bgcolor="#FFCC00"><strong>名称</strong></td>
<td width="142" align="center" valign="middle" bgcolor="#FFCC00"><strong>地址</strong></td>
<td width="118" align="center" valign="middle" bgcolor="#FFCC00" ><strong>时间</strong></td>
<td width='70' align="center" valign="middle" bgcolor="#FFCC00"><strong>线路</strong></td>
<td width='217' align="center" bgcolor="#FFCC00"><strong>版本信息</strong></td>
<td width="65" align="center" bgcolor="#FFCC00"><strong>星级</strong></td>
<td width="35" align="center" bgcolor="#FFCC00"><strong>介绍</strong></td>
</tr>页面上有很多这样的表,怎么先把这些表取出来呢?
string yourStr = richTextBox1.Text;
MatchCollection mc = Regex.Matches(yourStr, @"<table[^>]*?>[\s\S]*?</table>", RegexOptions.IgnoreCase);
foreach (Match m in mc)
{
richTextBox2.Text += m.Value + "\n";
}取得表中的内容也可以如上,替换一下其中的正则,如果要提取的表格格式统一,不必先取出表格,直接就可以取出其内容了
想取得上面的表格的各列的数据,,我想要存到数据库。。
string start = textBox1.Text; //表格开头信息,如“昨天已经开机私服信息列表”,“所有私服信息列表”。
string content = "";
提取相应表格
Match m = Regex.Match(yourStr, @"(?<=<div[^>]*?>[^<]*?"+start+@"[\s\S]*?)<table[^>]*?>[\s\S]*?</table>", RegexOptions.IgnoreCase);
if (m.Success)
{
content = m.Value;
}
//提取表格内容
MatchCollection mc = Regex.Matches(content, @"<td[^>]*?>(<[^>]*?>)?(.*?<a[^>]*?>)?( \s*)?([^<]*?)(</[^>]*?>)?</td>", RegexOptions.IgnoreCase);
foreach (Match ma in mc)
{
richTextBox2.Text += ma.Groups[4].Value + "\n";
}
这里表头信是是手动输入的,当然也可以先通过正则取出来存数组,我没有做,自己做下吧,下面的家族信息列表同样的道理
string start = textBox1.Text; //表格开头信息,如“昨天已经开机私服信息列表”,“所有私服信息列表”。
string content = "";
if(Regex.IsMatch(start, @"今日新开"))
{
Match m = Regex.Match(yourStr, @"(?<=<table[\s\S]*?<\!-- 今日新开私服信息)[\s\S]*?</table>", RegexOptions.IgnoreCase);
if (m.Success)
{
content = m.Value;
}
MatchCollection mc = Regex.Matches(content, @"<td[^>]*?>(( \s*|\s*)?(<[^>]*?>)?)*([^<]*?)\s*(<(?!/td>)[\s\S]*?)?</td>", RegexOptions.IgnoreCase);
foreach (Match ma in mc)
{
richTextBox2.Text += ma.Groups[4].Value + "\n";
}
}
else
{
Match m = Regex.Match(yourStr, @"(?<=<div[^>]*?>[^<]*?" + start + @"[\s\S]*?)<table[^>]*?>[\s\S]*?</table>", RegexOptions.IgnoreCase);
if (m.Success)
{
content = m.Value;
}
MatchCollection mc = Regex.Matches(content, @"<td[^>]*?>(( \s*|\s*)?(<[^>]*?>)?)*([^<]*?)(<[\s\S]*?)?</td>", RegexOptions.IgnoreCase);
foreach (Match ma in mc)
{
richTextBox2.Text += ma.Groups[4].Value + "\n";
}
}
全部测试过了,你再看下吧
onmouseout="javascript:this.bgColor='#FFFF99'" bgColor=#ffcc33>
<TD align=middle width=134> <A href="html/38862.htm"
target=show>傲天传世</A></TD>
<TD width=142><A href="http://www.atwoool.cn" target=show><FONT
color=#000000>http://www.atwoool.cn</FONT></A> </TD>
<TD width=118> 3月25日14点开放</TD>
<TD align=middle width=70>四川电信</TD>
<TD align=middle width=217>轻微变态</TD>
<TD align=right width=65>☆☆☆☆</TD>
<TD align=middle width=35><A href="html/38862.htm" target=show>查看</A></TD></TR>@"<td[^>]*?>(( \s*|\s*)?(<[^>]*?>)?)*([^<]*?)(<[\s\S]*?)?</td>"
用这个正则表达式,四川电信和☆☆☆☆匹配不出来,谁能帮帮忙。
string content = "";
if (Regex.IsMatch(start, @"今日新开"))
{
Match m = Regex.Match(yourStr, @"(?<=<table[\s\S]*?<\!-- 今日新开私服信息)[\s\S]*?</table>", RegexOptions.IgnoreCase);
if (m.Success)
{
content = m.Value;
}
}
else
{
Match m = Regex.Match(yourStr, @"(?<=<div[^>]*?>[^<]*?" + start + @"[\s\S]*?)<table[^>]*?>[\s\S]*?</table>", RegexOptions.IgnoreCase);
if (m.Success)
{
content = m.Value;
}
}MatchCollection mc = Regex.Matches(content, @"<td[^>]*?>(( \s*|\s*)?(<[^>]*?>)?)*([^<]*?)\s*(<(?!/td>)[\s\S]*?)?</td>", RegexOptions.IgnoreCase);
foreach (Match ma in mc)
{
richTextBox2.Text += ma.Groups[4].Value + "\n";
}