title: Parsing filenames with python's re module
body: <p>I'm developing video player which scans user's video files and tries to identify them from the filename. If the video is movie, I would like to get it's name and video's quality and if its a tv show I would like to get the show's name, season's number, episode's number and video's quality.</p>
  
  <p>I googled some example filenames and made simple script which tries to get the information, but I'm having really hard time dealing with exceptions such as when there is episode's name before quality, when the file has tag like "PROPER" or if the user has put the videos's source like "BluRay" before quality.</p>
  
  <p>I would really appreciate, if someone more skilled with regular expressions would give some help.</p>
  
  <p>Thanks!</p>
  
  <pre class="lang-python prettyprint-override"><code>import re
  names = [
      "The.Newsroom.2012.S02E06.720p.HDTV.x264-KILLERS.mkv",
      "Breaking.Bad.S05E10.Buried.HDTV.XviD-AFG.avi",
      "Breaking.Bad.S05E10.Buried.720p.HDTV.x264-AFG.mkv", #Incorrectly nonHD
      "Dexter.S08E08.HDTV.XviD-AFG.avi",
      "Dexter.S08E07.1080p.HDTV.x264-QCF.mkv",
      "Dexter S08E07 720p HDTV x264-QCF.mkv",
      "The.Great.Gatsby.2013.BluRay.1080p.DTS.x264-CHD.mkv", #Incorrectly nonHD
      "The Forbidden Girl 2013 BRRIP Xvid AC3-BHRG.avi",
      "Pain.&amp;.Gain.2013.720p.BluRay.DD5.1.x264-HiDt.mkv",
      "Band.of.Brothers.S01E02.Day.of.Days.DVDRip.XviD-AC3-BAGS.avi",
      "Dexter.S08E06.PROPER.720p.HDTV.x264-IMMERSE.mkv", #Incorrectly nonHD
      "Dexter S08E06 PROPER 720p HDTV x264-IMMERSE.mkv" #Incorrectly nonHD
  ]
  for name in names:
      tv = re.findall(r"(.*?)[ |.]S([\d+]{1,2})E([\d+]{1,2})[ |.]([\d+]{3,4}p|)", name) #FIXME: Get quality also after "PROPER/EPTITLE/.."
      if len(tv)&gt;0:
          print("---------- TV ----------")
          print("Show: "+tv[0][0].replace(".", " "))
          print("Season: "+str(int(tv[0][1])))
          print("Episode: "+str(int(tv[0][2])))
          print("Quality: "+(tv[0][3] if len(tv[0][3])&gt;0 else "nonHD"))
      else:
          movie = re.findall(r"(.*?[ |.][\d+]{4})[ |.]([\d+]{3,4}p|)", name) #FIXME: Get quality also after "BluRay/HDTV/HDDVD/.."
          if len(movie)&gt;0:
              print("--------- MOVIE --------")
              print("Title: "+movie[0][0].replace(".", " "))
              print("Quality: "+(movie[0][1] if len(movie[0][1])&gt;0 else "nonHD"))
          else:
              print("error")
  </code></pre>