Regex pattern that does not match certain extensions?

I have this pattern written


However there is a problem - this pattern matches (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.)


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

Use os.path's nifty functions to properly split up the filepath into components for easier parsing:

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

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

Try this regex (don't do it. It does the exact opposite of what you want to do):


If you only care that the string doesn't end with .jpg or .png, you can use this:


The ^.+ isn't strictly necessary, but depending on how the JSON parser is coded you might need to force the regex to consume the whole string. If you're using the regex for other validations as well, you might want something more elaborate, like:


You probably tried to use (?<!\.jpg|\.png), which wouldn't work because Python's regex flavor is one of the most restrictive when it comes to lookbehinds. PHP and Ruby 1.9+ would accept it because each of the alternatives has a fixed length. They don't even have to be the same length; (?<!\.jpg|\.jpeg|\.png) would work, too. Just don't try to factor out the dot, as in (?<!\.(?:jpg|jpeg|png)); the alternation has to be at the top level of the lookbehind.

Java would accept the factored-out version because it does a little more work at compile time to determine the maximum number of characters the lookbehind might need to match. The lookbehind expression needs to be fairly simple though, and it can't use the + or * quantifiers. Finally, the .NET and JGSoft flavors place no restrictions at all on lookbehinds. But Python makes a very simple-minded attempt to figure out the exact number of characters the lookbehind needs to match, generating that cryptic error message when it fails.

Please try


It will correctly match on filename.jpg. your trying to figure out how to make match ANY .jpg files even if the file's name has 2 or more dots in it, will work fine.
While using python script be sure that you are using the right type of split. the different type of split viz rsplit(right split) and lsplit(left split).

Looks like you almost had it:


According to my tests (in java) I get these results:

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

If this is not what you wanted pleas update your question with your expectations.

Category:python Time:2012-04-07 Views:0

Related post

  • REGEX Pattern - How do I match upto a certain tag in html 2012-01-05

    I have some html which I want to grab between 2 tags. However nested tags exist in the html so looking for wouldn't work as it would return on the first nested div. Basically I want my regex to.. Match some text literally, followed by ANY character u

  • Should this regex pattern throw an exception? 2011-06-03

    Should this regex pattern throw an exception? Does for me. ^\d{3}[a-z] The error is: parsing "^\d{3}[a" - Unterminated [] set. I feel dumb. I don't get the error. (My RegexBuddy seems okay with it.) A little more context which I hope doesn't cloud th

  • Java - how to match regex Pattern containing single quotes? 2009-06-07

    [EDITED - really sorry, the code I quoted was wrong - have changed the message below to reflect this. Apologies! Thank you for your patience.] I'm new to regular expressions and want to match a pattern in Java (following on from this solution - http:

  • Regex Pattern Matching 2009-11-19

    Possible Duplicate: Regex Pattern for a File Name A user can put a file in the server if the file name matches the following criteria: It has to be abc or it should start with abc, then a dot, and a number. Valid file names: abc abc.2344 abc.111 Inva

  • Regex help: My regex pattern will match invalid strings 2010-01-01

    i really like Regex, unfortantly Im not that good at it yet. So therfore I hope you guys can help me out. The text string I want to validate consists of what I call "segments". A single segment might look like this: [A-Z,S,3] So far I managed to buil

  • Matcher returns matches on a regex pattern, but split() fails to find a match on the same regex? 2010-01-31

    I can't see a reason why the Matcher would return a match on the pattern, but split will return a zero length array on the same regex pattern. It should return something -- in this example I'm looking for a return of 2 separate strings containing "pa

  • Java RegEx Pattern not matching (works in .NET) 2010-04-27

    Below is an example that is producing an exception in Java (and not matching the input). Perhaps I misunderstood the JavaDoc, but it looks like it should work. Using the same pattern and input in C# will produce a match. import java.util.regex.Matche

  • How can I match on, but exclude a regex pattern? 2010-06-09

    I have this URL: And this regex pattern: cID=[^&]* Which produces this result: cID=87B6XYZ964D293CF How do I REMOVE the "cID="? Thanks -------------

  • RegEx pattern not showing matches 2010-06-29

    I have the following code: public void DriveRecursion(string retPath) { string pattern = @"[~#&!%\+\{\}]+"; Regex regEx = new Regex(pattern); string[] fileDrive = Directory.GetFiles(retPath, "*.*", SearchOption.AllDirectories); List<string>

  • regex pattern to match alternating subpatterns 2010-08-20

    I'm trying to devise a regex pattern (in PHP) which will allow for any alternation of two subpatterns. So if pattern A matches a group of three letters, and B matches a group of 2 numerals, all of these would be OK: aaa aaa66bbb 66 67abc 12abc34def56

  • jQuery: How can I match text with a RegEx pattern and wrap the results in an anchor tag? 2010-09-25

    I have a bunch of tweets that are returned as plain text that I would like to go through and assign proper links tags to based on RegEx matches. As an example here is a tweet where I would like @Bundlehunt to become <a href="

  • Detect words or any character after some match pattern, Regex Pattern (Vim) 2010-09-26

    I have a text file patterned like this : 1 textA == this is textA == 1.1 textB === this is textB === 2 textC == this is textC == 2.1 textD === this is textD === 2.1.1 textE ==== this is textE ==== Whats the right regex pattern to formatting the text

  • How can I use lookbehind in a C# Regex in order to skip matches of repeated prefix patterns? 2010-10-01

    How can I use lookbehind in a C# Regex in order to skip matches of repeated prefix patterns? Example - I'm trying to have the expression match all the b characters following any number of a characters: Regex expression = new Regex("(?<=a).*"); for

  • Replacing character with File.separator using java.regex Pattern Matcher 2011-01-22

    I have a field called 'path' in back end database, which stores the path to certain resource. Instead of storing lots of backslashed (escaped) path for windows path, my idea is to let user enter the path with certain character as file separator (inde

  • Regex pattern matches fine but output is not complete 2011-02-13

    I am trying this regex pattern: $string = '<div class="className">AlwaysTheSame:</div>Subtitle <br /><span class="anotherClass">entry1</span><span class="anotherClass">entry2</span><span class="anotherClas

  • Regex Pattern Matching: Using only the start and end of a pattern for matching 2011-03-02

    Hi I am really struggling with this problem; I am trying to use regex to match the start and end of a pattern where the middle of the pattern can change. So I would like to use a regex expression that will match the start which is constant and the en

  • Regex help: My regex pattern will match invalid Dictionary 2011-04-01

    I hope you guys can help me out. I'm using C# .Net 4.0 I want validate file structure like const string dataFileScr = @" Start 0 { Next = 1 Author = rk Date = 2011-03-10 /* Description = simple */ } PZ 11 { IA_return() } GDC 7 { Message = 6 Message =

  • How to get match regex pattern using awk from file? 2011-04-04

    I need to find in file word that matches regex pattern. It must to be using awk... I ONLY want to print word matched with pattern! So if in line, i have: xxx yyy zzz And pattern: /yyy/ I wonna only get: yyy EDIT: thanks to kurumi i managed to write s

  • Regex - pattern for matching Alphabet characters between brackets 2011-04-04

    Can anyone assist in providing a Regex pattern to find letters between brackets? I.E: [A] or [AAA] or [BAB] The string I'm searching for could be something like this: ([A] * [B] + [AAA]) From that I'd like to be able to get the A, B, AAA out of the s

Copyright (C), All Rights Reserved.

processed in 3.029 (s). 13 q(s)