The kind of sentences expected :
I will be there at 4:15 later today
I will be there at 14:06 later today.
Few things to notice there:
1- The hour format is 24h
2- The hour can be one letter or 2 while the minutes is always 2.
3- The time is always mid sentence .
what i have tried :
import re
response = re.sub(r'^(([01]\d|2[0-3]):([0-5]\d)|24:00)$',
'7:15', 'I will be there at `4:15` later today')
I also tried this regex ^(2[0-3]|[01]?[0-9]):([0-5]?[0-9])$
and it also didn't work.
A couple of issues here but this works:
import re
response = re.sub(r'(?:[01]?\d|2[0-3]):(?:[0-5]\d)|24:00', '7:15', 'I will be there at `4:15` later today')
print(response)
This yields
I will be there at `7:15` later today
You need to get rid of the anchors and make [01] optional with a ?. Lastly, change the parentheses to non-capturing and remove non-necessary parentheses.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With