【发布时间】:2012-02-14 21:19:44
【问题描述】:
我两天前开始学习正则表达式,现在我想制作一个小应用程序,它可以读取网页的源代码并获取http://page.com或http://www.page.com/sub/sub/sub?=value等网页......之类的东西,无论如何,这是我输入的代码:
Regex r = new Regex("http://\\w");
HttpWebRequest httpwebrequest = (HttpWebRequest)WebRequest.Create("http://maktoob.yahoo.com/?p=us");
HttpWebResponse response = (HttpWebResponse)httpwebrequest.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
string line;
while ((line = sr.ReadLine()) != null)
{
Match m = r.Match(line);
if (m.Success)
{
Console.WriteLine("Match: " +m.Value);
}
}
sr.Close();
response.Close();
但结果是:
匹配:http://l 比赛:http://w 匹配:http://x 比赛:http://l 匹配:http://q
它只是获取 // 之后的第一个字符 当我查看我的模式时,我说大声笑是的,我的模式是 http://\w,所以它会得到第一个字符,但我想知道我应该在我的模式中添加什么才能获得其余的链接? ???
【问题讨论】:
标签: c# regex web-crawler web-scraping