Python/Regex - How to extract date from filename using regular expression?
Solution 1:
Assuming the date is always in the format: [MM]-[DD]-[YYYY].
re.search("([0-9]{2}\-[0-9]{2}\-[0-9]{4})", fileName)
Solution 2:
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.