Skip to content

Instantly share code, notes, and snippets.

@bradwright
Created June 2, 2010 09:37
Show Gist options
  • Save bradwright/422153 to your computer and use it in GitHub Desktop.
Save bradwright/422153 to your computer and use it in GitHub Desktop.
import re
pattern = re.compile('^.*?((?<!\.[a-z0-9]{8})png|jpg|gif|swf|ico|flv|zip)$')
# I want the first two to match, but not the second two
file_list = [
'main.ie6.zip',
'main.zip',
'main.cb1c4aa1.zip',
'main.cb1c4aa1.cb1c4aa1.zip'
]
for f in file_list:
print f
if pattern.match(f):
print pattern.match(f).groups()
@evilstreak
Copy link

The lack of a dot before the extension and the fact that the lookbehind is in one of the multiple options in the final matching set is, I think, what causes the failure. This works for me:

pattern = re.compile('^.*?(?<!\.[a-z0-9]{8}).(png|jpg|gif|swf|ico|flv|zip)$')

@bradwright
Copy link
Author

Yeah that's what Norm came up with too:

pattern = re.compile('^.*?(?<!\.[a-z0-9]{8})\.(png|jpg|gif|swf|ico|flv|zip)$')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment