用正则表达式,匹配网页元素

publicstaticstringGetHttp(stringurl)

{

HttpWebRequesthttpWebRequest=(HttpWebRequest)WebRequest.Create(url);

httpWebRequest.ContentType="application/json";

httpWebRequest.Method="GET";

httpWebRequest.Timeout=20000;

HttpWebResponsehttpWebResponse=(HttpWebResponse)httpWebRequest.GetResponse();

StreamReaderstreamReader=newStreamReader(httpWebResponse.GetResponseStream());

stringresponseContent=streamReader.ReadToEnd();

httpWebResponse.Close();

streamReader.Close();

returnresponseContent;

}

staticvoidMain(string[]args)

{

stringurl="http://wap.10086.cn/czjf/result.jsp";

stringhtml=GetHttp(url);

Regexr;//声明一个Regex类的变量

r=newRegex("<p.*?>(.+?)</p>");//定义表达式

Matchm=r.Match(html);//在字符串中匹配

if(m.Success)

{

r=newRegex(">(.+?)<");//找到后再定义表达式

MatchnewString=r.Match(m.ToString());

Console.Write(newString.ToString().Substring(1,10));

Console.ReadKey();

}

else

{

Console.Write("未找到");

Console.ReadKey();

}

}

通过匹配,找到自己想要的特定字符串

相关推荐