Python/Regex - 如何使用正则表达式从文件名中提取日期? [英] Python/Regex - How to extract date from filename using regular expression?
本文介绍了Python/Regex - 如何使用正则表达式从文件名中提取日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用 python 从文件名中提取日期.日期格式如下:
I need to use python to extract the date from filenames. The date is in the following format:
month-day-year.somefileextension
示例:
10-12-2011.zip
somedatabase-10-04-2011.sql.tar.gz
提取这个的最好方法是使用正则表达式?
The best way to extract this would be using regular expressions?
我有一些代码:
import re
m = re.search('(?<=-)\w+', 'derer-10-12-2001.zip')
print m.group(0)
代码将打印10".关于如何打印日期的一些线索?
The code will print '10'. Some clue on how to print the date?
此致,
推荐答案
假设日期总是采用以下格式:[MM]-[DD]-[YYYY].
Assuming the date is always in the format: [MM]-[DD]-[YYYY].
re.search("([0-9]{2}\-[0-9]{2}\-[0-9]{4})", fileName)
这篇关于Python/Regex - 如何使用正则表达式从文件名中提取日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文