爬天气预报的正则表达式【雏形】(来字WW.YESUR.COM)

类别:Asp 点击:0 评论:0 推荐:

//test Regex
   string content = "";
   string outHtml = "";
   Regex regex;
   Match mc;
   StreamReader sr = new StreamReader("c:\\a.txt",System.Text.Encoding.GetEncoding("gb2312"));
   content = sr.ReadToEnd();
   content = content.Replace("\"","'");
   
   //开始用正则表达式取出图片文件名

    //regex = new Regex("class=\"text02\">(?<CityName>.*)</div>");
   regex = new Regex(@"(\s*)<td width='105' bgcolor='F6F6F6' class='text02'>(\s*)(?<CityName>.*)(\s*)</td>(\s*)<td width='55' align='center' bgcolor='F6F6F6'>(\s*)(?<weather>.*)(\s*)</td>",RegexOptions.Compiled);

   outHtml += "<table widht=100% border=1 bgcolor=d7d7d7>";
   outHtml += "<tr><td colspan=10>test</td></tr>";
   for (mc = regex.Match(content); mc.Success; mc = mc.NextMatch())
   {
    outHtml += "<tr><td>城市:</td><td>" + mc.Groups["CityName"].Value + "</td><td>" + mc.Groups["weather"].Value + "</d></tr>";
   }
   outHtml += "</table>";

   Response.Write(outHtml);

 

*******************************

关键的一个地方是:

<tr><td>之间必须用(\s*)来表示,这个表示无限个空格,否则匹配不到,COOL

本文地址:http://com.8s8s.com/it/it7881.htm