fork download
  1. import re
  2.  
  3. exceptions = ['1st','2nd','10th','100th','1st nation','xlr8','5pin','h20', '12th'] # '12th' added
  4. exception_rx = '|'.join(map(re.escape, exceptions))
  5. generic_rx = r'(?<=\d)(?=[^\d\s])|(?<=[^\d\s])(?=\d)'
  6. rx = re.compile(rf'({exception_rx})|{generic_rx}', re.I)
  7.  
  8. string_lst = ['1ST DEF 100CD','ABC 1ST 100CD','WEST 12TH APARTMENT']
  9. for s in string_lst:
  10. print(rx.sub(lambda x: x.group(1) or " ", s))
Success #stdin #stdout 0.02s 9516KB
stdin
Standard input is empty
stdout
1ST DEF 100 CD
ABC 1ST 100 CD
WEST 12TH APARTMENT