fork download
  1. import re
  2. text = 'am pm P.M. 10:56pm 10:43 a.m.'
  3. rx = r'\b((?:[01]?[0-9]|[2][0-3]):[0-5][0-9]\s*[pa]\.?m\b\.?)|\b[ap]\.?m\b\.?'
  4. result = re.sub(rx, lambda x: x.group(1) if x.group(1) else "\n", text, flags=re.I)
  5. print(result)
Success #stdin #stdout 0.01s 27712KB
stdin
Standard input is empty
stdout
 
  
  10:56pm 10:43 a.m.