fork download
  1. import re
  2.  
  3. sss = ['AB.224-QW-2018', 'AB.876-5-LS-2018', 'AB.26-LS-18', 'AB-123-6-LS-2017', 'IA-Mb-22L-AB.224-QW-2018-IA-Mb-22L',
  4. 'ZX-ss-12L-AB-123-6-LS-2017-BC-22', 'AB.224-2018', 'AB.224/QW/2018', 'AB/224/2018',
  5. 'AB-10-HDB-231-NCLT-1-2017 AD-42-HH-2019', 'AB-1-HDB-NCLT-1-2016 AD-42-HH-2020',
  6. 'AB-1-HDB-NCLT-1-2016/(AD-42-HH-2020)']
  7.  
  8. pattern = r"(AB)\D*(\d+)\S*?(\d\d)\b(?=\s|[,'/]|-[A-Z]{2}\b|$)"
  9. for s in sss:
  10. m = re.search(pattern, s)
  11. if m:
  12. b = m.group(1) + "/" + m.group(2) + "/" + m.group(3)
  13. print(b)
  14. else:
  15. print('No Match:' + s)
Success #stdin #stdout 0.03s 9680KB
stdin
Standard input is empty
stdout
AB/224/18
AB/876/18
AB/26/18
AB/123/17
AB/224/18
AB/123/17
AB/224/18
AB/224/18
AB/224/18
AB/10/17
AB/1/16
AB/1/16