匹配所有网址,不包括jpg,gif,png

时间:2012-06-25 22:00:56

标签: java php regex match imageurl

我想匹配所有网址但排除与该正则表达式匹配的图片网址:jpe?g | gif | png。

\ B(:HTTPS | | FTP文件?):// [?!-A-Z0-9 +&安培; @#/%=〜_ | $:,;。] * A-Z0-9 +&安培; @#/%=〜_ | $

问题是带有排除的部分不是这样的:(?!jpe?g | gif | png)

有没有人有解决方案?

示例:

不是Matchen:

http://example.com/example.jpg
http://example.com/example231/example.gif

匹配度:

http://example.com/example.html
http://example.com/example/?id=4331
http://example.com/example/example/ex_ample/ex-ample/?id=4331  

2 个答案:

答案 0 :(得分:3)

只需使用(?!.*(?:\.jpe?g|\.gif|\.png)$)

启动正则表达式

因此,如果您当前的正则表达式为\b(?:https?|ftp|file)://...,请将其合并到

(?!.*(?:\.jpe?g|\.gif|\.png)$)\b(?:https?|ftp|file)://...

另请阅读PHP URL validation

答案 1 :(得分:0)

我正在解决这个问题,因为最近一个重复的问题已经关闭,所以我会在这里发布:

((?!.*(png|jpg|gif)(?!.))(?:https?|file|ftp):\/\/.*.\.(?:com|ca|net|museum|org|co\.uk))

如果合适,可以随意添加更多原型,如果合适,可以添加更多图片扩展名,如果合适,可以添加更多顶级域名。

Tested on regex101

相关问题