0

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  
Ωmega
  • 42,614
  • 34
  • 134
  • 203
Nicy
  • 11
  • 4

2 Answers2

3

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

Community
  • 1
  • 1
Ωmega
  • 42,614
  • 34
  • 134
  • 203
0

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

Devon Parsons
  • 1,234
  • 14
  • 23