I'm trying to create a regular expression that will detect when someone says "2 weeks from today, 2 days from today, etc.) Someone can also replace today with now. My current regular expression looks like this:
(re.compile(
r'''^
((?P<weeks>\d+) \s weeks?)?
[^\d]*
((?P<days>\d+) \s days?)?
[^\d]*
((?P<hours>\d+) \s hours?)?
[^\d]*
((?P<minutes>\d+) \s minutes?)?
[^\d]*
((?P<seconds>\d+) \s seconds?)?
\s
from [today, now]
''',
(re.VERBOSE | re.IGNORECASE)),
lambda m: datetime.datetime.today() + datetime.timedelta(
days=int(m.group('days') or 0),
seconds=int(m.group('seconds') or 0),
minutes=int(m.group('minutes') or 0),
hours=int(m.group('hours') or 0),
weeks=int(m.group('weeks') or 0)))
Whenever I run this regex, I get null because it can't detect "2 weeks from today" or something similar. How can I make it so that the output is anything 2 * [days, seconds, minutes, hours, weeks] from today or now?