I am trying to use regex to do the following in a string :
- If there is a hyphen
-
between two alphabets, we have to remove it:- Example
A-BA
should beABA
; andA-B-BAB
should beABBAB
- Example
- If an alphabet and a number are next to each other, then we have to insert a hyphen
-
symbol between them:- Example
9AHYA7
should be9-AHYA-7
; and977AB99T5
should be977-AB-99-T-5
- Example
These patterns are just simple examples. The string could be more complicated like this :
HS98743YVJUHGF78BF8HH3JHFC83438VUN5498FCNG
7267-VHSBVH8737HHC8C-HYHFWYFHH-7Y84743YR8437G
In the above strings the same principles have to be incorporated.
I tried the following code to convert 8T
into 8-T
re.sub(r'\dab-d', '\d-ab-d', s)
Unfortunately it does not work. I am not sure how to do it.