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

前端 未结 5 1152
滥情空心
滥情空心 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

    well the \w+ you put in matches one or more word characters following a hypen, so that's the expected result. What you want to do is use a lookaround on either side, matching numbers and hyphens that occur between the first hyphen and a period:

    re.search(r'(?<=-)[\d-]+(?=\.)', name).group(0)

提交回复
热议问题