You're working with dates, so simple arithmetic is not going to be enough. You'll need to extract and convert those strings to datetime
, and then add days using timedelta
. You can use re.sub
with a callback to facilitate this easily.
import re
import datetime
from datetime import datetime as dt
def foo(m):
date_string = dt.strptime(m.group(0), '%Y-%m-%d')
return (date_string + datetime.timedelta(days=5).strftime('%Y-%m-%d')
x = "These are the dates 2017-09-23 and 2017-01-03"
y = re.sub('\d{4}(?:-\d{2}){2}', foo, x)
print(y)
Output
These are the dates 2017-09-28 and 2017-01-08
Regex
\d{4} # 4 digits
(?: # non-capturing group
- # hyphen
\d{2} # 2 digits
){2} # group must appear twice