求一能识别URL的正则

shoushii 2010-02-05 10:13:26
能匹配http://www.163.com/a.aspx?p%id=1&id=1
能匹配http://test/a/a/a.aspx?p%id=1&id=1
不匹配包含在A链接里的URL,默认是关闭标签<a href="http://www.163.com" target="" alt="">http://www.163.com</a>

完整示例如下,加粗部分将被匹配:
http://www.163.com/a.aspx?p%id=1&id=1,http://www.163.com/a.aspx?p%id=1&id=1.aspx?http://www.163.com/a.aspx?p%id=1&id=1 http://test. http://test, http://test http://test/a/a/a.aspx?p%id=1&id=1.<a href="http://www.1.1" name="1">http://www.1.1</a>.aspx?
...全文
186 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
shoushii 2010-02-06
  • 打赏
  • 举报
回复
不错,两位的实现都不错。给分。
jiashu912387 2010-02-06
  • 打赏
  • 举报
回复
留个脚印 标记下
polarissky 2010-02-06
  • 打赏
  • 举报
回复
学习!
xuebond 2010-02-06
  • 打赏
  • 举报
回复
学习
mbh0210 2010-02-06
  • 打赏
  • 举报
回复
楼主试试过客的。
-过客- 2010-02-06
  • 打赏
  • 举报
回复
try...

Regex reg = new Regex(@"(?is)(?<!<a[^>]*?)https?://[^/\s]+(?<!\.)(/([\w-]+/)*([\w-]+\.[\w-]+(\?([^=,.]+=[^&,\s.]+)+)?)?)?(?!(?:(?!</?a\b).)*</a>)");
shoushii 2010-02-06
  • 打赏
  • 举报
回复
上面的都会match
http://test.
而不是
http://test
swalp 2010-02-06
  • 打赏
  • 举报
回复
不会正则啊
RexZheng 2010-02-05
  • 打赏
  • 举报
回复
(?<!<a [^>]+>(?=[^<]*</a>)|<a [^>]+)http://[^/ ]+(/?(\w+\/)*(\w+\.+\w+((\?|&)([^ ,&=.]+=[^ ,&=.]+)?)+)?)?


-过客- 2010-02-05
  • 打赏
  • 举报
回复
完善了一下,加入对http://test.com/abc.aspx这种情况的处理
不过量词嵌套太多,不好说效率会如何

string test = "http://www.163.com/a.aspx?p%id=1&id=1,http://www.163.com/a.aspx?p%id=1&id=1.aspx?http://www.163.com/a.aspx?p%id=1&id=1 http://test. http://test/,http://test.com/abc.aspx. http://test http://test/a/a/a.aspx?p%id=1&id=1. <a href=\"http://www.1.1\" name=\"1\">http://www.1.1 </a>.aspx?";
Regex reg = new Regex(@"(?is)(?<!<a[^>]*?)https?://[^/\s]+(/([\w-]+/)*([\w-]+\.[\w-]+(\?([^=,.]+=[^&,\s.]+)+)?)?)?(?!(?:(?!</?a\b).)*</a>)");
MatchCollection mc = reg.Matches(test);
foreach (Match m in mc)
{
richTextBox2.Text += m.Value + "\n";
}
/*-------输出--------
http://www.163.com/a.aspx?p%id=1&id=1
http://www.163.com/a.aspx?p%id=1&id=1
http://www.163.com/a.aspx?p%id=1&id=1
http://test.
http://test/
http://test.com/abc.aspx
http://test
http://test/a/a/a.aspx?p%id=1&id=1
*/
-过客- 2010-02-05
  • 打赏
  • 举报
回复
先别睡啊,等着你验证呢,呵呵

string test = "http://www.163.com/a.aspx?p%id=1&id=1,http://www.163.com/a.aspx?p%id=1&id=1.aspx?http://www.163.com/a.aspx?p%id=1&id=1 http://test. http://test/, http://test http://test/a/a/a.aspx?p%id=1&id=1. <a href=\"http://www.1.1\" name=\"1\">http://www.1.1 </a>.aspx?";
Regex reg = new Regex(@"(?is)(?<!<a[^>]*?)https?://[\w-]+((\.[\w-]+)+)?/?([\w-]+/)*([\w-]+\.[\w-]+\?([^=,.]+=[^&,\s.]+)+)?(?!(?:(?!</?a\b).)*</a>)");
MatchCollection mc = reg.Matches(test);
foreach (Match m in mc)
{
richTextBox2.Text += m.Value + "\n";
}
//输出
http://www.163.com/a.aspx?p%id=1&id=1
http://www.163.com/a.aspx?p%id=1&id=1
http://www.163.com/a.aspx?p%id=1&id=1
http://test
http://test/
http://test
http://test/a/a/a.aspx?p%id=1&id=1
shoushii 2010-02-05
  • 打赏
  • 举报
回复
先睡了,自己写了好久没成功。明早来看,期待高手。
shoushii 2010-02-05
  • 打赏
  • 举报
回复
完整示例如下,将匹配多次,每种颜色为一次单独的匹配,其中http://test形式为内网URL:
http://www.163.com/a.aspx?p%id=1&id=1,http://www.163.com/a.aspx?p%id=1&id=1.aspx?http://www.163.com/a.aspx?p%id=1&id=1 http://test. http://test/, http://test http://test/a/a/a.aspx?p%id=1&id=1. <a href="http://www.1.1" name="1">http://www.1.1 </a>.aspx?
RexZheng 2010-02-05
  • 打赏
  • 举报
回复
楼主分多啊,一个正则300分
shoushii 2010-02-05
  • 打赏
  • 举报
回复
补充例子为多个匹配,每种颜色为一种匹配
补充例子为多个匹配,每种颜色为一匹配
shoushii 2010-02-05
  • 打赏
  • 举报
回复
http://test将被识别为URL(内网URL)
补充例子为多个匹配,每种颜色为一种匹配
悔说话的哑巴 2010-02-05
  • 打赏
  • 举报
回复
关注,坐等空军大哥来
-过客- 2010-02-05
  • 打赏
  • 举报
回复
不是很清楚需求
http://test,
这种不符合URL规范,仅仅是举例,还是真实存在的?

加粗部分被匹配,匹配结果是加粗部分整体,还是按http://***这种形式每个单独匹配
shoushii 2010-02-05
  • 打赏
  • 举报
回复
http://www.163.com/a.aspx?p%id=1&id=1,http://www.163.com/a.aspx?p%id=1&id=1.aspx?http://www.163.com/a.aspx?p%id=1&id=1 http://test. http://test/, http://test http://test/a/a/a.aspx?p%id=1&id=1. <a href="http://www.1.1" name="1">http://www.1.1 </a>.aspx?
wiki14 2010-02-05
  • 打赏
  • 举报
回复
关注。

62,046

社区成员

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

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

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

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