(Darn, John beat me up. Well, you can see examples anyway)
Like the other guys, regex is not the best tool for this job. If you work with file paths, see os.path .
As for filtering files you donโt need, you can do if 'thumb' not in filename: ... after you split the path (where filename is str ).
And for posterity, here are my thoughts on this regular expression. r".*(?!thumb).*" does not work because .* is greedy and lookahead has a very low priority. Take a look at this:
>>> re.search('(.*)((?!thumb))(.*)', '/tmp/somewhere/thumb').groups() ('/tmp/somewhere/thumb', '', '') >>> re.search('(.*?)((?!thumb))(.*)', '/tmp/somewhere/thumb').groups() ('', '', '/tmp/somewhere/thumb') >>> re.search('(.*?)((?!thumb))(.*?)', '/tmp/somewhere/thumb').groups() ('', '', '')
The latter is rather strange ...
Another regular expression ( r"^(?!.*thumb).*" ) Works because .* Is inside lookahead, so you have no problem with stolen characters. In fact, you donโt even need ^ , depending on whether you re.match or re.search :
>>> re.search('((?!.*thumb))(.*)', '/tmp/somewhere/thumb').groups() ('', 'humb') >>> re.search('^((?!.*thumb))(.*)', '/tmp/somewhere/thumb').groups() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'NoneType' object has no attribute 'groups' >>> re.match('((?!.*thumb))(.*)', '/tmp/somewhere/thumb').groups() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'NoneType' object has no attribute 'groups'