How to switch two items in a row using Python RegEx?

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.

+5
source share
2 answers

One option is to commit the template between two interested templates and use the backlink to change them:

 s = r'xyzA*12*pqR*stB*HS*lmN*opA*45*a4N*gB*SD*drtU*ghy' import re pattern = re.compile(r'A\*(.*?)(\*.*?B\*)(.*?)\*') #keep the position of the second group and switch the position of the first and third group pattern.sub(r"A*\3\2\1*", s) # 'xyzA*HS*pqR*stB*12*lmN*opA*SD*a4N*gB*45*drtU*ghy' 
+8
source

Alternatively, you can also solve this by searching for a string and loop instead of regular expressions:

 str = r'xyzA*12*pqR*stB*HS*lmN*opA*45*a4N*gB*SD*drtU*ghy' result = "" start = 0 while (str.find("A*", start) > -1): aStart = str.find("A*", start)+2 aEnd = str.find("*", aStart) bStart = str.find("B*", aEnd)+2 bEnd = str.find("*", bStart) result += str[start:aStart]+str[bStart:bEnd]+str[aEnd:bStart]+str[aStart:aEnd] start = bEnd result += str[start:] print result #xyzA*HS*pqR*stB*12*lmN*opA*SD*a4N*gB*45*drtU*ghy 
+1
source

Source: https://habr.com/ru/post/1263977/


All Articles