正则表达式求助

natfit 2005-09-29 11:04:46
有一篇天气预报,要取其中一截:

用如下reg:<div id=成都[\s\S]*<td class="w08">([\s\S]*)</td>

只取到了最后郑州的一组,
9月29日-9月30日<br>
小雨转中雨<br>
22 ~ 16 ℃<br>
无风向微风

如果想取成都的这一块,该用那种方式,多谢!

9月29日-9月30日<br>
多云转阵雨<br>
25 ~ 18 ℃<br>
无风向微风

文章示范如下:


/div>
<div id=重庆 style="position:absolute; left:338; top:313; width:138; height:136; z-index:1; background-color: ; layer-background-color: ; border: 1px none #000000; visibility: hidden;">
<div align="center">
<table width="247" cellspacing="0" cellpadding="0">

<tr>
<td height="24" align="center" background="./templates/images/popup_01.gif" class="w05">重庆</td>
</tr>
<tr>
<td valign="top"><img src="./templates/images/popup_02.jpg" width="247" height="15"></td>
</tr>
<tr>
<td background="./templates/images/popup_04.jpg"><table width="100%" cellspacing="0" cellpadding="0">
<tr valign="top">
<td width="12"><img src="" width="12" height="1"></td>

<td class="w03"><table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td width="80"><img src="../images/a1.gif"></td>
<td class="w08">9月29日-9月30日<br>
多云<br>
32 ~ 23 ℃<br>
无风向微风</td>

</tr>
</table></td>
<td width="12"><img src="" width="12" height="1"></td>
</tr>
</table></td>
</tr>
<tr>
<td><img src="./templates/images/popup_03.jpg" width="247" height="8"></td>
</tr>

<tr>
<td><img src="./templates/images/popup_06.gif" width="247" height="25"></td>
</tr>
</table>
</div>
</div>
<div id=成都 style="position:absolute; left:312; top:303; width:138; height:136; z-index:1; background-color: ; layer-background-color: ; border: 1px none #000000; visibility: hidden;">
<div align="center">
<table width="247" cellspacing="0" cellpadding="0">

<tr>
<td height="24" align="center" background="./templates/images/popup_01.gif" class="w05">成都</td>
</tr>
<tr>
<td valign="top"><img src="./templates/images/popup_02.jpg" width="247" height="15"></td>
</tr>
<tr>
<td background="./templates/images/popup_04.jpg"><table width="100%" cellspacing="0" cellpadding="0">
<tr valign="top">
<td width="12"><img src="" width="12" height="1"></td>

<td class="w03"><table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td width="80"><img src="../images/a1.gif"><br><img src="../images/a3.gif"></td>
<td class="w08">9月29日-9月30日<br>
多云转阵雨<br>
25 ~ 18 ℃<br>
无风向微风</td>

</tr>
</table></td>
<td width="12"><img src="" width="12" height="1"></td>
</tr>
</table></td>
</tr>
<tr>
<td><img src="./templates/images/popup_03.jpg" width="247" height="8"></td>
</tr>

<tr>
<td><img src="./templates/images/popup_06.gif" width="247" height="25"></td>
</tr>
</table>
</div>
</div>
<div id=郑州 style="position:absolute; left:409; top:253; width:138; height:136; z-index:1; background-color: ; layer-background-color: ; border: 1px none #000000; visibility: hidden;">
<div align="center">
<table width="247" cellspacing="0" cellpadding="0">

<tr>
<td height="24" align="center" background="./templates/images/popup_01.gif" class="w05">郑州</td>
</tr>
<tr>
<td valign="top"><img src="./templates/images/popup_02.jpg" width="247" height="15"></td>
</tr>
<tr>
<td background="./templates/images/popup_04.jpg"><table width="100%" cellspacing="0" cellpadding="0">
<tr valign="top">
<td width="12"><img src="" width="12" height="1"></td>

<td class="w03"><table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td width="80"><img src="../images/a7.gif"><br><img src="../images/a8.gif"></td>
<td class="w08">9月29日-9月30日<br>
小雨转中雨<br>
22 ~ 16 ℃<br>
无风向微风</td>
...全文
168 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
natfit 2005-09-30
  • 打赏
  • 举报
回复
搞了半天,原来是他们又把页面改掉了,把class="w08"改成class="cn"了!

多谢nico_angel(天使联盟) !!!
natfit 2005-09-29
  • 打赏
  • 举报
回复
实际上就是要取第一个匹配的
nico_angel 2005-09-29
  • 打赏
  • 举报
回复
那就应该没有问题的,我后面那种方法应该是可以的,自己试一试。开动脑筋
natfit 2005-09-29
  • 打赏
  • 举报
回复
无论如何,先结贴
natfit 2005-09-29
  • 打赏
  • 举报
回复
是debug加的
nico_angel 2005-09-29
  • 打赏
  • 举报
回复
表达式应该没有问题,不知道你有没有换掉里面的回车符号

\"cn\">其中的“\"”和“\r\n”是文本中的,还是debug中程序转义的
natfit 2005-09-29
  • 打赏
  • 举报
回复
总是取不到,我说搞定是在一个reg工具中测试的,没有在c#中运行

现在放到程序中,用直接从网页上取的数据,就不成功。

原来测试的数据是我从页面的源文件中copy出来的,现在的是直接取的
nico_angel 2005-09-29
  • 打赏
  • 举报
回复
\\s*用的不对,不需要\\s转义
string rul1="class=\"w05\">成都</td>[^月]+?<td class=\"w08\">([^<]+)<br>\\s*([^<]+)<br>\\s*([^<]+)<br>\\s*([^<]+)</td>\\s*</tr>";
Regex r=new Regex(rul1,RegexOptions.IgnoreCase);
//string content= r.Match (page_content).Value ;

Match m=r.Match(page_content);

if (m.Success)
{

wt.date=m.Groups[1].ToString();
wt.weather =m.Groups[2].ToString();
wt.temperature =m.Groups[3].ToString();
wt.wind=m.Groups[4].ToString();

}

正确方式
string rul1=@"class=.w05.>成都</td>[^月]+?<td class=.w08.>([^<]+)<br>\s*([^<]+)<br>\s*([^<]+)<br>\s*([^<]+)</td>\s*</tr>";
Regex r=new Regex(@rul1,RegexOptions.IgnoreCase);
//string content= r.Match (page_content).Value ;

Match m=r.Match(page_content);

if (m.Success)
{

wt.date=m.Groups[1].ToString();
wt.weather =m.Groups[2].ToString();
wt.temperature =m.Groups[3].ToString();
wt.wind=m.Groups[4].ToString();

}
natfit 2005-09-29
  • 打赏
  • 举报
回复
受益非浅!

现在就是不知道为什么在c#中执行时不能成功,m.Success 总是不对,是不是还需要修改什么参数?
nico_angel 2005-09-29
  • 打赏
  • 举报
回复
这是你DEBUG时取出来得数据吧,请问你想作什么呢!
“.”可以匹配(空格类字符意外的字符)。所有他匹配“"”
nico_angel 2005-09-29
  • 打赏
  • 举报
回复
这是为了好看一点,当然你也可以用\"。
natfit 2005-09-29
  • 打赏
  • 举报
回复
页面内容:page_content(是程序运行中取出来的)

<tr> \r\n <td width=\"80\"><img src=\"../images/a0.gif\"></td>\r\n <td class=\"cn\">9月29日-9月30日<br>\r\n 晴<br> \r\n 30 ~ 22 ℃<br>\r\n 微风</td>\r\n\r\n </tr>\r\n </table></td>\r\n <td width=\"12\"><img src=\"\" width=\"12\" height=\"1\"></td>\r\n </tr>\r\n </table></td>\r\n </tr>\r\n <tr> \r\n <td><img src=\"./templates/images/popup_03.jpg\" width=\"247\" height=\"8\"></td>\r\n </tr>\r\n\r\n <tr> \r\n <td><img src=\"./templates/images/popup_06.gif\" width=\"247\" height=\"25\"></td>\r\n </tr>\r\n</table>\r\n </div>\r\n</div>\r\n<div id=重庆 style=\"position:absolute; left:338; top:313; width:138; height:136; z-index:1; background-color: ; layer-background-color: ; border: 1px none #000000; visibility: hidden;\"> \r\n <div align=\"center\">\r\n <table width=\"247\" cellspacing=\"0\" cellpadding=\"0\">\r\n\r\n <tr> \r\n <td height=\"24\" align=\"center\" background=\"./templates/images/popup_01.gif\" class=\"w05\">重庆</td>\r\n </tr>\r\n <tr> \r\n <td valign=\"top\"><img src=\"./templates/images/popup_02.jpg\" width=\"247\" height=\"15\"></td>\r\n </tr>\r\n <tr> \r\n <td background=\"./templates/images/popup_04.jpg\"><table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">\r\n <tr valign=\"top\"> \r\n <td width=\"12\"><img src=\"\" width=\"12\" height=\"1\"></td>\r\n\r\n <td class=\"big-cn\"><table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\r\n <tr> \r\n <td width=\"80\"><img src=\"../images/a1.gif\"></td>\r\n <td class=\"cn\">9月29日-9月30日<br>\r\n 多云<br> \r\n 32 ~ 23 ℃<br>\r\n 微风</td>\r\n\r\n </tr>\r\n </table></td>\r\n <td width=\"12\"><img src=\"\" width=\"12\" height=\"1\"></td>\r\n </tr>\r\n </table></td>\r\n </tr>\r\n <tr> \r\n <td><img src=\"./templates/images/popup_03.jpg\" width=\"247\" height=\"8\"></td>\r\n </tr>\r\n\r\n <tr> \r\n <td><img src=\"./templates/images/popup_06.gif\" width=\"247\" height=\"25\"></td>\r\n </tr>\r\n</table>\r\n </div>\r\n</div>\r\n<div id=成都 style=\"position:absolute; left:312; top:303; width:138; height:136; z-index:1; background-color: ; layer-background-color: ; border: 1px none #000000; visibility: hidden;\"> \r\n <div align=\"center\">\r\n <table width=\"247\" cellspacing=\"0\" cellpadding=\"0\">\r\n\r\n <tr> \r\n <td height=\"24\" align=\"center\" background=\"./templates/images/popup_01.gif\" class=\"w05\">成都</td>\r\n </tr>\r\n <tr> \r\n <td valign=\"top\"><img src=\"./templates/images/popup_02.jpg\" width=\"247\" height=\"15\"></td>\r\n </tr>\r\n <tr> \r\n <td background=\"./templates/images/popup_04.jpg\"><table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">\r\n <tr valign=\"top\"> \r\n <td width=\"12\"><img src=\"\" width=\"12\" height=\"1\"></td>\r\n\r\n <td class=\"big-cn\"><table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\r\n <tr> \r\n <td width=\"80\"><img src=\"../images/a1.gif\"><br><img src=\"../images/a3.gif\"></td>\r\n <td class=\"cn\">9月29日-9月30日<br>\r\n 多云转阵雨<br> \r\n 25 ~ 18 ℃<br>\r\n 微风</td>\r\n\r\n </tr>\r\n </table></td>\r\n <td width=\"12\"><img src=\"\" width=\"12\" height=\"1\"></td>\r\n </tr>\r\n </table></td>\r\n </tr>\r\n <tr> \r\n <td><img src=\"./templates/images/popup_03.jpg\" width=\"247\" height=\"8\"></td>\r\n </tr>\r\n\r\n <tr> \r\n <td><img src=\"./templates/images/popup_06.gif\" width=\"247\" height=\"25\"></td>\r\n </tr>\r\n</table>\r\n </div>\r\n</div>\r\n<div id=郑州 style=\"position:absolute; left:409; top:253; width:138; height:136; z-index:1; background-color: ; layer-background-color: ; border: 1px none #000000; visibility: hidden;\"> \r\n <div align=\"center\">\r\n <table width=\"247\" cellspacing=\"0\" cellpadding=\"0\">\r\n\r\n <tr> \r\n <td height=\"24\" align=\"center\" background=\"./templates/images/popup_01.gif\" class=\"w05\">郑州</td>\r\n </tr>\r\n <tr> \r\n <td valign=\"top\"><img src=\"./templates/images/popup_02.jpg\" width=\"247\" height=\"15\"></td>\r\n </tr>\r\n <tr> \r\n <td background=\"./templates/images/popup_04.jpg\"><table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">\r\n <tr valign=\"top\"> \r\n <td width=\"12\"><img src=\"\" width=\"12\" height=\"1\"></td>\r\n\r\n <td class=\"big-cn\"><table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\r\n <tr> \r\n <td width=\"80\"><img src=\"../images/a7.gif\"><br><img src=\"../images/a8.gif\"></td>\r\n <td class=\"cn\">9月29日-9月30日<br>\r\n 小雨转中雨<br> \r\n 22 ~ 16 ℃<br>\r\n 微风</td>\r\n\r\n </tr>\r\n </table></td>\r\n <td width=\"12\"><img src=\"\" width=\"12\" height=\"1\"></td>\r\n </tr>\r\n </table></td>\r\n </tr>\r\n <tr> \r\n <td><img src=\"./templates/images/popup_03.jpg\" width=\"247\" height=\"8\"></td>\r\n </tr>\r\n\r\n <tr> \r\n <td><img src=\"./templates/images/popup_06.gif\" width=\"247\" height=\"25\"></td>\r\n </tr>\r\n</table>\r\n </div>\r\n</div>\r\n<div id=西安 style=\"position:absolute; left:362; top:260; width:138; height:136; z-index:1; background-color: ; layer-background-color: ; border: 1px none #000000; visibility: hidden;\"> \r\n <div align=\"center\">\r\n <table width=\"247\" cellspacing=\"0\" cellpadding=\"0\">\r\n\r\n <tr> \r\n <td height=\"24\" align=\"center\" background=\"./templates/images/popup_01.gif\" class=\"w05\">西安</td>\r\n </tr>\r\n <tr> \r\n <td valign=\"top\"><img src=\"./templates/images/popup_02.jpg\" width=\"247\" height=\"15\"></td>\r\n </tr>\r\n <tr> \r\n <td background=\"./templates/images/popup_04.jpg\"><table width=\"100%\
natfit 2005-09-29
  • 打赏
  • 举报
回复
<td height=.24. align=.center.
你为什么要用.来代替"

我的reg,在.net中不对,很怪,

string rul1="class=\"w05\">成都</td>[^月]+?<td class=\"w08\">([^<]+)<br>\\s*([^<]+)<br>\\s*([^<]+)<br>\\s*([^<]+)</td>\\s*</tr>";
Regex r=new Regex(rul1,RegexOptions.IgnoreCase);
//string content= r.Match (page_content).Value ;

Match m=r.Match(page_content);

if (m.Success)
{

wt.date=m.Groups[1].ToString();
wt.weather =m.Groups[2].ToString();
wt.temperature =m.Groups[3].ToString();
wt.wind=m.Groups[4].ToString();

}
nico_angel 2005-09-29
  • 打赏
  • 举报
回复
natfit(natfit) ,不好意思,上面一段回错了。


[\s\S]*匹配所有字符,而且贪婪的,所有你就匹配到最后一个了。
[^月]+?只是一个限定而已,其实其他的字符都可以,其中的月可以用不是
<td height=.24. align=.center. background=../templates/images/popup_01.gif. class=.w\d+.>([^>]+)</td>到<td class=.w\d+.>9月29日-9月30日<br>字符中间的任意一个字符,例如,我你年日等。在这只是为了加快匹配的速度。减省严格的匹配带来的速度问题
nico_angel 2005-09-29
  • 打赏
  • 举报
回复
刚才我没有上线,看来你只要成都一个城市的内容啊
string city="";
string datetime="";
string weather="";
string temperature="";
string others="";
Regex re=new Regex(@"<td height=.24. align=.center. background=../templates/images/popup_01.gif. class=.w\d+.>([^>]+)</td>[^月]+?<td class=.w\d+.>\d+月\d+日\-\d+月\d+日<br>\s*成都<br>\s*([^<]+)<br>\s*([^<]+)</td>\s*</tr>");
Match m=re.Match(content);
if(m.Success)
{
city=m.Groups[1].ToString();//城市
datetime=m.Groups[2].ToString();
weather=m.Groups[3].ToString();
temperature=m.Groups[4].ToString();
others=m.Groups[5].ToString();
}
wuyi8808 2005-09-29
  • 打赏
  • 举报
回复
[^月] 表示除'月'以外的字符
+ 表示一次或多次重复
? 表示懒惰匹配, 匹配到最少符合要求的就停止
natfit 2005-09-29
  • 打赏
  • 举报
回复
如果我用[\s\S]*就不行,而>[^月]+?就可以,为什么?
natfit 2005-09-29
  • 打赏
  • 举报
回复
搞定:
class="w05">成都</td>[^月]+?<td class="w08">([^<]+)<br>\s*([^<]+)<br>\s*([^<]+)<br>\s*([^<]+)</td>\s*</tr>

能讲讲这个[^月]+?的意思吗?
wuyi8808 2005-09-29
  • 打赏
  • 举报
回复
在</td>前面加个?试试:
<div id=成都[\s\S]*<td class="w08">([\s\S]*)?</td>
natfit 2005-09-29
  • 打赏
  • 举报
回复
re : nico_angel(天使联盟)

你这个把所有的城市都取出来了,还需要循环去找!如果我把城市的位置改成“成都”,就一个都没有

有没有能一下就把成都的取出来的?

我就是搞不明白为什么条件后面的</td>会取到最后面去,而不是第一个碰到的

我先钻研一下你的reg再说,还是很不错的!
加载更多回复(2)

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧