.net正则匹配a标签
2009-08-26 16:00:51| 分类: Asp.Net |字号 订阅//c#通过正则匹配html中的a标签 的方法public string matchA(string teststr) { StringBuilder sb = new StringBuilder(); //关键就是这个里面的这则表达式 Regex reg = new Regex(@"<\s*a\shref=*[^>]*>([^<]|<(?!/a))*<\s*/a\s*>"); MatchCollection match = reg.Matches(teststr); foreach (Match var in match) { if (var != null) { sb.Append(Server.HtmlEncode(var.Value)); sb.Append("<br />"); } } return sb.ToString(); }//这个是测试方法获取网易首页的a标签所有信息public void test(){ string temp=string.Empty; WebRequest webReq = WebRequest.Create(" http://www.163.com"); WebResponse webRes = webReq.GetResponse(); Stream resStream = webRes.GetResponseStream(); StreamReader sr = new StreamReader(resStream, Encoding.Default); StringBuilder sb = new StringBuilder(); while ((temp = sr.ReadLine()) != null) { sb.Append(temp); } webRes.Close(); sr.Close(); //打印出所有匹配信息 Response.Write(matchA(sb.ToString())); }