Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to switch two elements in string using Python RegEx?

Tags:

python

regex

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.

like image 249
harshal1618 Avatar asked Feb 08 '17 15:02

harshal1618


People also ask

How do you change a string pattern in Python?

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.

How do you replace all occurrences of a regex pattern in a string in Python?

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.

How do you split a string in regex in Python?

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.


1 Answers

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'
like image 57
Psidom Avatar answered Oct 14 '22 19:10

Psidom