I am trying to switch two elements in a string while keeping all other characters intact. Here is an example:
Source line:
r'xyzA*12*pqR*stB*HS*lmN*opA*45*a4N*gB*SD*drtU*ghy'
Required Conclusion:
r'xyzA*HS*pqR*stB*12*lmN*opA*SD*a4N*gB*45*drtU*ghy'
Please note that the item after A * and B * is switched.
I managed to compile a RegEx template that allows me to replace elements as follows:
>>> import re >>> pattern = re.compile(r'A\*(.*?)\*.*?B\*(.*?)\*') >>> M = pattern.findall(string) >>> M [('12', 'HS'), ('45', 'SD')]
After this step, I need your help to find out how to use sub to get the required string.
source share