I'd like to implement a sed-like search-and-replace in Python.
Now obviously, Python has the re module:
import re
re.sub("([A-Z]+)", r"\1-\1", "123 ABC 456")
However, I would like to specify the search/replace operation in a single string, like in sed (leaving aside any escaping issues for now):
s/([A-Z]+)/\1-\1/g
The reason, I prefer this syntax, is because the actual search&replacement specification is supplied by the user, and I think it is simpler for the user to specify a single search/replace string, rather than both a pattern and a template.
Update
I'm only interested in sed's s (search/replace) command, for single lines (no special extensions).
The use-case is really to allow users to provide a string-transformation (with groups) for hostnames.
Any ideas?
sedcommands? there are quite a lot. What aboutgoption or not? this is too broad right now (means: a lot of code would have to be written to convert sed expression into search & replace python expression). How far do you want to go . rewritesed.py? or simpler?