Python

Moderators: None (Apply to moderate this forum)
Number of threads: 400
Number of posts: 1055

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
match extensions with regular expresion Posted by alastors on 8 Feb 2010 at 3:48 PM
hello to all!

I'm new to python , worked with regexp in javascript only, and I'm stuck with one little problem, I want to know if File has extension: .zip .7z .gz .tar ...(only Compressed Files).

But if it's .rar, it must not be xxx.partN.rar ('N' is a number and 'xxx' can be anything) UNLESS 'N' is '1' or '01' '001' etc.

Any help will be greatly appreciated.
Report
Re: match extensions with regular expresion Posted by bubbatremell on 19 Feb 2010 at 4:59 PM
: hello to all!
:
: I'm new to python , worked with regexp in javascript only, and I'm
: stuck with one little problem, I want to know if File has extension:
: .zip .7z .gz .tar ...(only Compressed Files).
:
: But if it's .rar, it must not be xxx.partN.rar ('N' is a number and
: 'xxx' can be anything) UNLESS 'N' is '1' or '01' '001' etc.
:
: Any help will be greatly appreciated.
:
import re
rar = re.compile(".+\.part[^(0*1)]\.rar") #invalid rar's
ext_list = ['zip','7z', 'gz', 'tar', 'rar'] #and whatever else

def foo(n):
  if n[n.rfind('.') + 1:] in ext_list: 
    if not n[-3:] == 'rar':
      return True                #valid not rar file
    elif rar.match(n):
      return False               #invalid rar file
    return True                  #valid rar file
  return False                   #not in ext_list
[\code]




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.