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

前端 未结 5 1188
滥情空心
滥情空心 2020-11-27 08:05

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

month-day-year.somefileextension

Examples:

5条回答
  •  甜味超标
    2020-11-27 08:57

    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.

提交回复
热议问题