Avenger
Avenger

Reputation: 441

All files ending with a suffix without using negation

I'm trying to match all files except those ending with .bmp.

Due to some constraints I can't use negation (?:, !:) and references (\1, ...).

I've made an expression and it works for most of the strings:

^\w+\.([^b].*|b|b[^m].*|bm|bm[^p].*|bmp.+)$

It matches everything that doesn't end with .bmp - including test.txt, test.bmp.txt, etc. But unfourtanely, it does allow test.bi.bmp.

Any idea on how to improve the regex so it would just match files not ending with .bmp?

Upvotes: 5

Views: 422

Answers (3)

Benoit
Benoit

Reputation: 79185

why not:

^.*[^p]$|^.*[^m]p$|^.*[^b]mp$|^.*[^.]bmp$

?

http://regexr.com?31vg7

An alternative is ^.*([^p]|[^m]p|[^b]mp|[^.]bmp)$ (shorter).

Upvotes: 5

fff
fff

Reputation: 11

/^.+\.([^b][^.]*|b|b[^m][^.]*|bm|bm[^p][^.]*|bmp[^.]+)$/

just make sure that the dot before 'bmp' is the last dot

Upvotes: 1

Dmitry
Dmitry

Reputation: 4442

Not too elegant but works:

^.*([^b][^m][^p]|b[^m][^p]|[^b]m[^p]|[^b][^m]p|bm[^p]|[^b]mp)$

Upvotes: 0

Related Questions