Reputation: 1136
There is a sample of regx: "one two" use this regx to \b(\w+)(\s)(\w+)\b replace with $3$2$1 and then get "two one" Now i want to implement this with python re.sub:
s="one two"
print re.sub(r"\b(\w+)(\s)(\w+)\b","$3$2$1",s)
This output $3$2$1, not "two one".How can i do this in python?
Upvotes: 1
Views: 266
Reputation: 59984
You use \number
in python for printing a matched result:
>>> s="one two"
>>> print re.sub(r"\b(\w+)(\s)(\w+)\b",r"\3\2\1",s)
two one
Note that you have to make it a raw string, or you would have to do \\3 \\2 \\1
(i.e escape the backslashes)
Upvotes: 2