Regex pattern that does not match certain extensions?

Shane Thornton picture Shane Thornton · Apr 7, 2012 · Viewed 18.2k times · Source

I have this pattern written

^.*\.(?!jpg$|png$).+$

However there is a problem - this pattern matches file.name.jpg (2 dots)

It works correctly (does not match) on filename.jpg. I am trying to figure out how to make it not match ANY .jpg files even if the file's name has 2 or more dots in it. I tried using a look behind but python complains about not using a fixed width (which I'm not exactly sure what that means, but the file name will be variable length.)

Answer

bereal picture bereal · Apr 7, 2012

This should work: ^.*\.(?!jpg$|png$)[^.]+$