繁体   English   中英

如何使用python中的re从文本中读取mmm-yy和mmm-yyyy格式的日期并进行数学运算

[英]How to read date in mmm-yy and mmm-yyyy format from text using re in python and do mathematical operations

string = "this is Aug-2010 date1 and this is 21-08-1990 date 2 "

我想阅读Aug-2010作为日期

我必须读取包含不同日期格式的文本数据的多个文件,我需要从该文本中提取日期并对其执行数学运算。 我可以读取数组中的日期并保存,无法对其执行数学运算:

例子

String = "I need to calculation the duration between Aug-88 and Jan-89"

date_type= re.compile(r'(\d{2}(/|-|\.)\w{3}(/|-|\.)\d{4})|(\w{3}(/|-
    |\.)\d{4})|(\w{3}(/|-|\.)\d{2})|))
arr=[]
for j in re.finditer(date_typ,string):
    arr.append(j.group())

现在进一步需要创建一个变量,该变量将在数组中的两个日期之间有所不同。

基于这样的事实,月份总是由 3 个字母组成,年份总是 4 位数字,它们总是用“-”分隔:

import re
regex_prog = re.compile(r"([A-Za-z]{3}-[0-9]{4})")
test_str = "this is Aug-2010 date1 and this is  21-08-1990 date 2 "
matches = regex_prog.search(test_str)
if matches:
    print(matches.group(0))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM