简体   繁体   English

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

[英]Match all URLs exclude jpg,gif,png

I want to match all URLS but exclude image urls from beeing matched with that regex: jpe?g|gif|png . 我想匹配所有URL,但从与该正则表达式匹配的beeing中排除图像URL:jpe?g | gif | png。

\\b(?:https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|$!:,.;]*A-Z0-9+&@#/%=~_|$ \\ b(?:https?| ftp | file):// [-A-Z0-9 +&@#/%?=〜_ | $!:,。;] * A-Z0-9 +&@# /%=〜_ | $

The problem is that the part with the exclude is not working like this: (?!jpe?g|gif|png) 问题是带有排除项的部分不能像这样工作:(?!jpe?g | gif | png)

Does anyone have a solution for it? 有人对此有解决方案吗?

Example: 例:

not Matchen: 不匹配:

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

Match: 比赛:

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

Just start your regex with (?!.*(?:\\.jpe?g|\\.gif|\\.png)$) , 只需使用(?!.*(?:\\.jpe?g|\\.gif|\\.png)$)开始您的正则表达式,

so if your current regex is \\b(?:https?|ftp|file)://... , then merge it to 因此,如果您当前的正则表达式为\\b(?:https?|ftp|file)://... ,则将其合并到

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

Read also PHP URL validation 另请阅读PHP URL验证

I was working on this problem for a recent duplicate question that got closed, so I'll post it here: 我正在为一个最近重复的问题解决这个问题,因此我将其发布在这里:

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

Feel free to add more protocals if appropriate, more image extensions if appropriate, and more top level domains if appropriate. 如果合适,可以随意添加更多协议,如果合适,可以添加更多图像扩展,如果合适,可以添加更多顶级域。

Tested on regex101 在regex101上测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM