Regex template that doesn't match specific extensions?

I wrote this template

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

However, there is a problem - this template corresponds to file.name.jpg (2 points)

It works correctly (does not match) on filename.jpg. I am trying to figure out how to do this does not correspond to ANY.jpg files, even if the file name has 2 or more points in it. I tried using the look and feel, but python complains that it is not using a fixed width (which I'm not quite sure what that means, but the file name will be variable length.)

+5
source share
5 answers

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

+10
source

Use os.pathgreat features to properly separate component paths for easier parsing:

filepath, filename = os.path.split(str)
basename, extension = os.path.splitext(filename)

if exension[1:] in ['jpg', 'png']:
  # The extension matches

( , , ):

\.(jpg|png)([^\.]|$)
+3

, :

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

( java), :

file.jpg - false
file.png - false
file.name.jpg - false
file.name.png - false
file.gif - true
file.name.gif - true
file.jpg.gif - true
file.jpge - true

, , .

+1

, .jpg .png, :

^.+$(?<!\.jpg)(?<!\.png)

^.+ , , JSON-, . , - , :

^\w+(?:\.\w+)+$(?<!\.jpg)(?<!\.png)

, (?<!\.jpg|\.png), , Python , lookbehinds. PHP Ruby 1.9+ , . ; (?<!\.jpg|\.jpeg|\.png) . , (?<!\.(?:jpg|jpeg|png)); lookbehind.

Java , , , . lookbehind , + *. , .NET JGSoft lookbehind. Python , lookbehind, , .

+1

    .*\.(jpg$|png$)

It will match correctly on filename.jpg. you are trying to figure out how to make ANY.jpg files, even if the file name has 2 or more points in it, it will work fine.
When using a python script, make sure you are using the correct type of separation. different type of split - rsplit (right split) and lsplit (left split).

0
source

All Articles