Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 混合字符和数字日期正则表达式_Python_Regex_Date_Text Mining - Fatal编程技术网

Python 混合字符和数字日期正则表达式

Python 混合字符和数字日期正则表达式,python,regex,date,text-mining,Python,Regex,Date,Text Mining,我需要找到一个Python正则表达式,以便匹配原始文本文件中的每个有效日期。我把文本分成几行,并把它们放在一个系列中,现在的目标是只提取每行中的日期,得到一系列的日期。我能够匹配大多数数字日期格式,但当我不得不处理文字月份(1月、1月、2月、2月……)时,我就停止了。特别是,我需要一个正则表达式(或一组正则表达式),它匹配以下格式: - Mar-20-2009; Mar 20, 2009; March 20, 2009; Mar. 20, 2009; Mar 20 2009; - 20 Mar

我需要找到一个Python正则表达式,以便匹配原始文本文件中的每个有效日期。我把文本分成几行,并把它们放在一个系列中,现在的目标是只提取每行中的日期,得到一系列的日期。我能够匹配大多数数字日期格式,但当我不得不处理文字月份(1月、1月、2月、2月……)时,我就停止了。特别是,我需要一个正则表达式(或一组正则表达式),它匹配以下格式:

- Mar-20-2009; Mar 20, 2009; March 20, 2009; Mar. 20, 2009; Mar 20 2009;
- 20 Mar 2009; 20 March 2009; 20 Mar. 2009; 20 March, 2009
- Mar 20th, 2009; Mar 21st, 2009; Mar 22nd, 2009
- Feb 2009; Sep 2009; Oct 2010
任何帮助都将不胜感激,
提前谢谢你

根据我的评论,建议使用split和strip从输出字符串生成一个可能的日期列表,然后将其提供给dateutils.parser.parse()以转换为适当的datetime对象,您可以根据自己的喜好进行操作

可能的执行情况如下:

test = '''- Mar-20-2009; Mar 20, 2009; March 20, 2009; Mar. 20, 2009; Mar 20 2009;
- 20 Mar 2009; 20 March 2009; 20 Mar. 2009; 20 March, 2009
- Mar 20th, 2009; Mar 21st, 2009; Mar 22nd, 2009
- Feb 2009; Sep 2009; Oct 2010'''
list_of_dates = []
for line in test.split('\n'):
    for date in line.split(';'):
        list_of_dates.append(date.strip(' - '))
from dateutil.parser import parse

def is_date(string):
    try: 
        parse(string)
        return True
    except ValueError:
        return False
found_dates = []
for date in list_of_dates:
    if is_date(date):
       found_dates.append(parse(date))
for date in found_dates:
    print(date)
结果:

2009-03-20 00:00:00
2009-03-20 00:00:00
2009-03-20 00:00:00
2009-03-20 00:00:00
2009-03-20 00:00:00
2009-03-20 00:00:00
2009-03-20 00:00:00
2009-03-20 00:00:00
2009-03-20 00:00:00
2009-03-20 00:00:00
2009-03-21 00:00:00
2009-03-22 00:00:00
2009-02-04 00:00:00
2009-09-04 00:00:00
2010-10-04 00:00:00

你需要使用正则表达式吗?这有一些库。首先,我将您的输出拆分为一个字符串列表,然后我将使用dateutils.parser.parse(字符串)将每个字符串转换为有效的datetime对象。请看这里的详细信息,谢谢,但这不是我的场景。我有一个系列,其中每一项都是一行文本,包含任何格式的日期。所以我不能拆分或剥离文本。