Python has re.escape() if you want to match an arbitrary string literal.
In my case, however, I want to use an arbitrary string literal as a replacement string in re.sub(). The re.escape() method produces excessive escaping for that, but an unescaped literal string is not safe to use, because there are still special characters.
Is there an equivalent of Java’s Matcher.quoteReplacement()?
Backslash is the only “special” character in a replacement, according to the re.sub documentation.
There’s only one obvious way to do it, plus a non-obvious but documented way: