Ryan
Ryan

Reputation: 10101

How to match file under a specific path but not particular extension using Regex

For example, to match all files (not jpg, png, gif) under the path common, e.g.

matched:

/common/foo.php
/common/foo.doc

not matched:

/common/foo.jpg
/common/foo.gif
/foo

Currently I am using:

\/common\/.*^(?:jpg|png|gif)$ 

Upvotes: 1

Views: 1054

Answers (2)

VBart
VBart

Reputation: 15110

location /common/ {
    # here configuration for not jpg, png, gif

    location ~ \.(?:png|gif|jpg)$ {
        # here configuration for jpg, png, gif
    }
}

Upvotes: 0

Tim
Tim

Reputation: 14154

A negative look-behind would be close to your current attempt:

\/common\/.*(?<!\.jpg|\.png|\.gif)$

This matches everything starting with "/common/", but not ending in ".jpg", ".png" or ".gif".

Demo

Upvotes: 1

Related Questions