I am trying to swap h and w in "hello World" as "wello horld" using back referencing. I am able to capture the group but something goes wrong when I refer the group in sub() method.
import re
st = "hello world"
t = re.compile(r"(\w).+\s(\w).+")
res = t.sub(r"\2 \1",st)
print(res)
I get output as "w h" instead of the desired string. What am I missing ?
t = re.compile(r"(\w)(.+\s)(\w)")
and replace withr"\3\2\1"