Python3 – extract file name

Intro

For some time now I stopped doing katas on codewars. So I have decided I will train now every week regularly. This kata is mainly about regular expressions which I knows pretty well.

Instructions

You have to extract a portion of the file name as follows:

  • Assume it will start with date represented as long number
  • Followed by an underscore
  • Youll have then a filename with an extension
  • it will always have an extra extension at the end

Inputs:

1231231223123131_FILE_NAME.EXTENSION.OTHEREXTENSION
1_This_is_an_otherExample.mpg.OTHEREXTENSIONadasdassdassds34
1231231223123131_myFile.tar.gz2/pre>

Outputs

FILE_NAME.EXTENSION
This_is_an_otherExample.mpg
myFile.tar

The recommend way to solve it is using RegEx and specifically groups.

My Solution

import re
class FileNameExtractor:
 def extract_file_name(dirty_file_name):
   return re.search('^\d+_(.*)\..*$', dirty_file_name).group(1)