fork download
  1. import re
  2.  
  3. pattern = r"([A-Z]-(?=[A-Z]))|([A-Z](?=[0-9])|[0-9](?=[A-Z]))"
  4. strings = [
  5. "A-BA",
  6. "A-B-BAB",
  7. "9AHYA7",
  8. "977AB99T5",
  9. "HS98743YVJUHGF78BF8HH3JHFC83438VUN5498FCNG",
  10. "7267-VHSBVH8737HHC8C-HYHFWYFHH-7Y84743YR8437G"
  11. ]
  12.  
  13. for str in strings:
  14. result = re.sub(
  15. pattern,
  16. lambda x: x.group(1)[:-1] if x.group(1) else x.group(2) + "-",
  17. str
  18. )
  19. print(result)
Success #stdin #stdout 0.02s 9588KB
stdin
Standard input is empty
stdout
ABA
ABBAB
9-AHYA-7
977-AB-99-T-5
HS-98743-YVJUHGF-78-BF-8-HH-3-JHFC-83438-VUN-5498-FCNG
7267-VHSBVH-8737-HHC-8-CHYHFWYFHH-7-Y-84743-YR-8437-G