Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/Regex - How to extract date from filename using regular expression?

Tags:

python

regex

I need to use python to extract the date from filenames. The date is in the following format:

month-day-year.somefileextension

Examples:

10-12-2011.zip
somedatabase-10-04-2011.sql.tar.gz

The best way to extract this would be using regular expressions?

I have some code:

import re
m = re.search('(?<=-)\w+', 'derer-10-12-2001.zip')
print m.group(0)

The code will print '10'. Some clue on how to print the date?

Best Regards,

like image 375
André Avatar asked Oct 11 '11 15:10

André


3 Answers

Assuming the date is always in the format: [MM]-[DD]-[YYYY].

re.search("([0-9]{2}\-[0-9]{2}\-[0-9]{4})", fileName)
like image 142
Dan Avatar answered Oct 03 '22 17:10

Dan


You want to use a capture group.

m = re.search('\b(\d{2}-\d{2}-\d{4})\.', 'derer-10-12-2001.zip')
print m.group(1)

Should print 10-12-2001.

You could get away with a more terse regex, but ensuring that it is preceded by a - and followed by a . provides some minimal protection against double-matches with funky filenames, or malformed filenames that shouldn't match at all.

EDIT: I replaced the initial - with a \b, which matches any border between an alphanumeric and a non-alphanumeric. That way it will match whether there is a hyphen or the beginning of the string preceding the date.

like image 20
Chriszuma Avatar answered Oct 03 '22 17:10

Chriszuma


I think you can extract the date using re.split as follows

$ ipython

In [1]: import re

In [2]: input_file = '10-12-2011.zip'

In [3]: file_split = re.split('(\d{2}-\d{2}-\d{4})', input_file, 1)

In [4]: file_split
Out[4]: ['', '10-12-2011', '.zip']

In [5]: file_split[1]
Out[5]: '10-12-2011'

In [6]: input_file = 'somedatabase-10-04-2011.sql.tar.gz'

In [7]: file_split = re.split('(\d{2}-\d{2}-\d{4})', input_file, 1)

In [8]: file_split
Out[8]: ['somedatabase-', '10-04-2011', '.sql.tar.gz']

In [9]: file_split[1]
Out[9]: '10-04-2011'

I ran the tests with Python 3.6.6, IPython 5.3.0

like image 35
Kamaraju Kusumanchi Avatar answered Oct 03 '22 15:10

Kamaraju Kusumanchi