I am trying to switch two elements in the string while keeping all the other characters untouched. Here is an example:
Original string:
r'xyzA*12*pqR*stB*HS*lmN*opA*45*a4N*gB*SD*drtU*ghy'
Required output:
r'xyzA*HS*pqR*stB*12*lmN*opA*SD*a4N*gB*45*drtU*ghy'
Notice that element after A* and B* are switched.
I was able to compile a RegEx pattern that gives me elements to replace like following:
>>> import re
>>> pattern = re.compile(r'A\*(.*?)\*.*?B\*(.*?)\*')
>>> M = pattern.findall(string)
>>> M
[('12', 'HS'), ('45', 'SD')]
After this stage I need your help to find out how to use sub to get the required string.
To replace a string in Python, the regex sub() method is used. It is a built-in Python method in re module that returns replaced string. Don't forget to import the re module. This method searches the pattern in the string and then replace it with a new given expression.
sub() method will replace all pattern occurrences in the target string. By setting the count=1 inside a re. sub() we can replace only the first occurrence of a pattern in the target string with another string. Set the count value to the number of replacements you want to perform.
Regex example to split a string into words In this example, we will split the target string at each white-space character using the \s special sequence. Let's add the + metacharacter at the end of \s . Now, The \s+ regex pattern will split the target string on the occurrence of one or more whitespace characters.
One option is to capture the pattern between the two interested patterns as well and use back reference to reorder 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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With