正则正确 可是只匹配了部分项 求解答

anhuicl 2009-04-30 04:32:59
//设置提取网页中链接的正则表达式
string param = "href=\"(?<link>[^\"]*)\">";
//提取网页中的链接
//stringline是我读入的网页字符串
MatchCollection mc = Regex.Matches(stringline, param, RegexOptions.IgnoreCase);

这个正则我用正则测试软件测试stringline可以得到581个匹配项
可是我取MC.COUNT只有28个 这是为什么啊
...全文
87 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
anhuicl 2009-04-30
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 wangjun8868 的回复:]
C# code
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://www.163.com");
request.Timeout = 20000;
request.ServicePoint.ConnectionLimit = 100;
request.ReadWriteTimeout = 30000;
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode != HttpStatusCode.OK)

[/Quote]
虽然不是我的问题的答案,但你的正则用起来是正常。。 谢谢
编程有钱人了 2009-04-30
  • 打赏
  • 举报
回复

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://www.163.com");
request.Timeout = 20000;
request.ServicePoint.ConnectionLimit = 100;
request.ReadWriteTimeout = 30000;
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode != HttpStatusCode.OK)
return;
StreamReader sr = new StreamReader(response.GetResponseStream());
Regex reg = new Regex(@"<a.*?href=""(?<href>[^""]*)""[^>]*>", RegexOptions.IgnoreCase);
MatchCollection mc = reg.Matches(sr.ReadToEnd()); //设定要查找的字符串
sr.Close();
foreach (Match m in mc)
{
if(m.Success)
{
Response.Write(m.Groups["href"].Value+"<br/>");
}

}
Response.End();
anhuicl 2009-04-30
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 gyouyang 的回复:]
这样看看 "href=\‘(? <link>[^\’]*)\‘>";
[/Quote]
这个是匹配单引号里的LINK 我要的是双引号里的。。。。。
anhuicl 2009-04-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zzjj999 的回复:]
RegexOptions.Singleline
[/Quote]
跟这个没有关系 我测试了
gyouyang 2009-04-30
  • 打赏
  • 举报
回复
这样看看 "href=\‘(?<link>[^\’]*)\‘>";
gyouyang 2009-04-30
  • 打赏
  • 举报
回复
汗...

肯定不同撒
anhuicl 2009-04-30
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 HDNGO 的回复:]
引用 2 楼 anhuicl 的回复:
单引号跟引号是不同的啊


对哇,你的那个正则只考虑了双引号的情况
[/Quote]
你好像没明白我的意思 我的那个正则用正则测试软件测试相同的stringline有581
但是我自己写的MatchCollection mc = Regex.Matches(stringline, param, RegexOptions.IgnoreCase); 只得到了581个匹配项
不是需要单引号的情况
zzjj999 2009-04-30
  • 打赏
  • 举报
回复
RegexOptions.Singleline
HDNGO 2009-04-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 anhuicl 的回复:]
单引号跟引号是不同的啊
[/Quote]

对哇,你的那个正则只考虑了双引号的情况
anhuicl 2009-04-30
  • 打赏
  • 举报
回复
单引号跟引号是不同的啊
HDNGO 2009-04-30
  • 打赏
  • 举报
回复
单引号。没引号~

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧