Search code examples
pythonpython-re

I want to find the word between the quotes and change the position


My plan is to find the bbb in the text and reposition it after extracting the words enclosed in quotes on both sides

text = "'Banana' aaaaa'Tomato' aaaaa'Tomato' aaaaa'Watermelon' aaaaaa 'Apple'bbb 'Banana' aaaa 'Orange' aaaaaa 'Melon' aaaaaa 'Persimmon' aaaaaaa 'Grape'bbb 'Apple' aaaaaa 'Grape' aaaaa'Tomato' aaaaa 'avocado' aaaaa 'watermelon' aaaaa 'apple'bbb 'mango' aaaa' orange' aaaaaa 'melon' aaaaaa 'persimmon' aaaaaaa 'plum' bbb 'tangerine' aaaaaa..."

For example, extract'Apple'bbb 'Banana', 'Grape'bbb 'Apple', 'apple'bbb 'mango', 'plum' bbb 'tangerine' from the text above and change the order to 'Banana'bbb 'Apple', 'Apple'bbb 'Grape', 'mango'bbb 'apple ','tangerine' bbb 'plum'

Is to change the text after making the change. So finally

text = "'Banana' aaaaa'Tomato' aaaaa'Tomato' aaaaa'Watermelon' aaaaaa 'Banana'bbb 'Apple' aaaa 'Orange' aaaaaa 'Melon' aaaaaa 'Persimmon' aaaaaaa 'Apple'bbb 'Grape' aaaaaa 'Grape' aaaaa'Tomato' aaaaa 'avocado' aaaaa 'watermelon' aaaaa 'mango'bbb 'apple' aaaa' orange' aaaaaa 'melon' aaaaaa 'persimmon' aaaaaaa 'tangerine' bbb 'plum' aaaaaa..."

Is to make it like this. What I have done so far is

text = "'Banana' aaaaa'Tomato' aaaaa'Tomato' aaaaa'Watermelon' aaaaaa 'Apple'bbb 'Banana' aaaa 'Orange' aaaaaa 'Melon' aaaaaa 'Persimmon' aaaaaaa 'Grape'bbb 'Apple' aaaaaa 'Grape' aaaaa'Tomato' aaaaa 'avocado' aaaaa 'watermelon' aaaaa 'apple'bbb 'mango' aaaa' orange' aaaaaa 'melon' aaaaaa 'persimmon' aaaaaaa 'plum' bbb 'tangerine' aaaaaa..."

p = re.compile(r"'(\S+\s*\S+\s*)bbb\s*'(\S+)'")
string = p.findall(text)

print(string)

a = list(string[0])
a.reverse()

b = list(string[1])
b.reverse()

print(a)
print(b)

I even extracted and changed the order, but I don't really know how to replace it.


Solution

  • You need to use re.sub to achieve this. Also you need to use \1 \2 \3 and so on. Like this

    re.sub(r"('\S*\s*\S+\s*')(bbb\s*)('\S+')", r"\3 \2 \1", text)
    

    The \1 and so on are the matches of your regex, as denoted by the parentheses. The second parameter of re.sub will construct the result.