fork download
  1. import re, datetime
  2.  
  3. input_text = "Alrededor de las 00:16 am o las 23:30 pm 2022_-_02_-_18 , quizas cerca del 2022_-_02_-_18 llega el avion, pero no (2022_-_02_-_18 20:16 pm) a las (2022_-_02_-_18 00:16 am), de esos hay dos (22)"
  4.  
  5. input_date_structure = r"(?P<year>\d*)_-_(?P<month>\d{2})_-_(?P<startDay>\d{2})"
  6.  
  7. identify_only_date_regex = r"(\b\d{2}:\d{2}[\s|]*[ap]m)?[\s|]*" + input_date_structure + r"[\s|]*(\b\d{2}:\d{2}[\s|]*[ap]m)?"
  8.  
  9. date_restructuring_structure = r"\g<year>_-_\g<month>_-_\g<startDay>"
  10. restructuring_only_date = lambda x: x.group() if x.group(1) or x.group(5) else "(" + x.expand(date_restructuring_structure) + " 00:00 am)"
  11.  
  12. input_text = re.sub(identify_only_date_regex, restructuring_only_date, input_text)
  13. print(repr(input_text)) # --> output
Success #stdin #stdout 0.03s 9776KB
stdin
Standard input is empty
stdout
'Alrededor de las 00:16 am o las 23:30 pm 2022_-_02_-_18 , quizas cerca del(2022_-_02_-_18 00:00 am)llega el avion, pero no (2022_-_02_-_18 20:16 pm) a las (2022_-_02_-_18 00:16 am), de esos hay dos (22)'