关于正则提取网址的问题,在线等~十万火急

怎么来这一出 2012-10-08 11:55:53
获取一个html源码,然后提取里面的符合条件的网址,比如http://style.sina.com.cn/industry/2012-10-08/1147106801.shtml这种,结尾是若干数字+“.shtml”结尾的网址,怎么用正则表达式做到呢?
...全文
147 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
怎么来这一出 2012-10-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lzxue1989 的回复:]

C# code

string s = ",比如http://style.sina.com.cn/industry/2012-10-08/1147106801.shtml 这种";
s = Regex.Match(s, @"[a-zA-z]+://[^\s]*").ToString();
System.Diagnostics.Debug.Pri……
[/Quote]不对啦,有很多网址:http://style.t.sina.com.cn/index.shtml http://roll.style.sina.com.cn/fas/jewelry/index.shtml http://roll.style.sina.com.cn/time/guide/index.shtml http://roll.style.sina.com.cn/tas/cigar/index.shtml http://roll.style.sina.com.cn/tas/club/index.shtml http://roll.style.sina.com.cn/lei/hotels/index.shtml http://roll.style.sina.com.cn/lei/cars/index.shtml http://roll.style.sina.com.cn/lei/yachting/index.shtml http://roll.style.sina.com.cn/des/architecture/index.shtml http://roll.style.sina.com.cn/industry/index.shtml http://style.sina.com.cn/industry/2012-10-08/1147106801.shtml我只需要数字+“.shtml”结尾的
csdn_风中雪狼 2012-10-08
  • 打赏
  • 举报
回复

string s = ",比如http://style.sina.com.cn/industry/2012-10-08/1147106801.shtml 这种";
s = Regex.Match(s, @"[a-zA-z]+://[^\s]*").ToString();
System.Diagnostics.Debug.Print(s);
MessageBox.Show(s);
// 输出
// http://style.sina.com.cn/industry/2012-10-08/1147106801.shtml

不好意思,一楼的没有测试,这用这个
csdn_风中雪狼 2012-10-08
  • 打赏
  • 举报
回复
([/w-]+/.)+[/w-]+.([^a-z])(/[/w- ./?%&=]*)?|[a-zA-Z0-9/-/.][/w-]+.([^a-z])(/[/w- ./?%&=]*)?

([/w-]+/.)+[/w-]+.([^a-z])(/[/w-: ./?%&=]*)?|[a-zA-Z0-9/-/.][/w-]+.([^a-z])(/[/w-: ./?%&=]*)?
怎么来这一出 2012-10-08
  • 打赏
  • 举报
回复
谢谢“lzxue1989”和“q107770540”两位的都很好用,结贴!
q107770540 2012-10-08
  • 打赏
  • 举报
回复
(?i)https?://\S*?/\d+\.shtml
csdn_风中雪狼 2012-10-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
引用 2 楼 lzxue1989 的回复:

C# code

string s = ",比如http://style.sina.com.cn/industry/2012-10-08/1147106801.shtml 这种";
s = Regex.Match(s, @"[a-zA-z]+://[^\s]*").ToString();
System.Diagnostics.Debug.P……
[/Quote]
匹配出来,在判断下
修改这样

string s = "http://style.t.sina.com.cn/index.shtml http://roll.style.sina.com.cn/fas/jewelry/index.shtml http://roll.style.sina.com.cn/time/guide/index.shtml http://roll.style.sina.com.cn/tas/cigar/index.shtml http://roll.style.sina.com.cn/tas/club/index.shtml http://roll.style.sina.com.cn/lei/hotels/index.shtml http://roll.style.sina.com.cn/lei/cars/index.shtml http://roll.style.sina.com.cn/lei/yachting/index.shtml http://roll.style.sina.com.cn/des/architecture/index.shtml http://roll.style.sina.com.cn/industry/index.shtml http://style.sina.com.cn/industry/2012-10-08/1147106801.shtml";

Regex re = new Regex(@"[a-zA-z]+://[^\s]*");

MatchCollection mc = re.Matches(s);

string t = string.Empty;

foreach (Match m in mc)
{
if (Regex.IsMatch(m.ToString(), @"[a-zA-z]+://[\s\S]*?/[0-9]+.shtml"))
{
System.Diagnostics.Debug.Print(m.ToString());
}
}
//输出 http://style.sina.com.cn/industry/2012-10-08/1147106801.shtml

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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