How Can I do vi-like substitutions?

Tim Peters tim_one at email.msn.com
Thu Sep 30 22:32:06 EDT 1999


[Michel Huster]
> I want to do some complex replace inside python. I am used to vi
> power like:
>
> s/\([0-9]*\) at \([0-9]*\)/\2 AT \1/
>
> which will change
> 1234 at 5678
> into
> 5678 AT 1234
>
> I can only find re.sub(expr, repl, count) but this does not 'remember' the
> saves matches between arguments.

?

>>> import re
>>> re.sub(r"(\d*) at (\d*)", r"\2 AT \1", "1234 at 5678")
'5678 AT 1234'
>>>

read-the-docs?-ly y'rs  - tim






More information about the Python-list mailing list