问题
I want to match all URLS but exclude image urls from beeing matched with that regex: jpe?g|gif|png .
\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)
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
回答1:
Just start your regex with (?!.*(?:\.jpe?g|\.gif|\.png)$)
,
so if your current regex is \b(?:https?|ftp|file)://...
, then merge it to
(?!.*(?:\.jpe?g|\.gif|\.png)$)\b(?:https?|ftp|file)://...
Read also PHP URL validation
回答2:
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
来源:https://stackoverflow.com/questions/11198001/match-all-urls-exclude-jpg-gif-png