简体   繁体   中英

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 "

I want to read Aug-2010 as date

I have to read multiple files with text data containing different date formats,I need to extract dates from that text and perform mathematical operation on that. I can read dates in array and save, Unable to perform mathematical operation on that:

Example

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())

Now further in need to create a variable that will be diff between two dates in the array.

Basing on fact that month always consists of 3 letters and year is always 4 digits and they are always separated with '-':

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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