怎么用正则表达式获取网页中的特定字符串
要获取 http://ieeexplore.ieee.org/xpl/articleDetails.jsp?arnumber=6574869 这个网页中的字符串“Performance Analysis and Optimization for Energy-Efficient Cooperative Transmission in Random Wireless Sensor Network”自己C#写了一段程序,但总是获取不到任何字符,请大家帮忙看一下,谢谢!
//获取指定字符串
private string GetPaperName(string strHTML)
{
Regex re = new Regex("(?<=<meta name=\"citation_title\" content=\").*\b");
Match ma = re.Match(strHTML);
string strName = ma.Value.ToString();
return strName;
}
--------------------编程问答--------------------
//获取网页源码
private string GetHttpWebRequest(string url)
{
Uri uri = new Uri(url);
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(uri);
myReq.UserAgent = "User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705";
myReq.Accept = "*/*";
myReq.KeepAlive = true;
myReq.Headers.Add("Accept-Language", "zh-cn,en-us;q=0.5");
HttpWebResponse result = (HttpWebResponse)myReq.GetResponse();
Stream receviceStream = result.GetResponseStream();
StreamReader readerOfStream = new StreamReader(receviceStream, System.Text.Encoding.GetEncoding("utf-8"));
string strHTML = readerOfStream.ReadToEnd();
readerOfStream.Close();
receviceStream.Close();
result.Close();
return strHTML;
}
//获取指定字符串--------------------编程问答--------------------
private string GetPaperName(string strHTML)
{
Regex re = new Regex("(?<=<meta name=\"citation_title\" content=\").*?(?=\")");
Match ma = re.Match(strHTML);
return ma.Success?ma.Value:"";
}
搞定了,谢谢~~
斑竹能解释下我那为什么错了,我用下载的一个正则表达式测试工具测试能得到正确结果,但程序中又不对了。
补充:.NET技术 , C#