Python's support for hexadecimal escapes in the replacement text
Question: Could not found a corresponding PEP or a bug for one problem in Python's `re`
module.

Does anyone know if the following is planned to be fixed?

From [regular-expressions.info](http://www.regular-
expressions.info/replacenonprint.html):

> Python does not support hexadecimal escapes in the replacement text syntax,
> even though it supports \xFF and \uFFFF in string constants.

But it actually supports standard escapes like `\n`, `\r`, etc.

So, for example one cannot replace `'<'` character with `'>'` character using
hexadecimal escapes:

    
    
    >>> import re
    >>> re.sub(r'\x3c', r'\x3e', '\x3c')
    '\\x3e'
    

Instead of `'\\x3e'` it should be `'>'`.

Using escaped `\n` works fine:

    
    
    >>> re.sub(r'a', r'\n', 'a')
    '\n'
    

Thanks in advance!

**UPD:** Not using the raw string is not an option. For example if pattern and
replacement strings are stored in a config file, so if I write \x3e in it, it
will become `'\\x3e'` when read, instead of `'>'`.


Answer: The only workaround I know if is to not use a raw string for the replacement
text and instead allow normal string evaluation to make `\x3e` into `>`. This
works because, as you noted, python strings do support such sequences.

    
    
    >>> import re
    >>> re.sub(r'\x3c', '\x3e', '\x3c')
    '>'
    

This means that in more complex replacement text you need more escapes, which
could make it less readable, but at least it works.

I don't know if there is any plan to improve on this. I took a look at the
existing documentation for the [python 3.4 re
module](http://docs.python.org/3.4/library/re.html#re.sub) (under dev) and
found no mention of including this kind of support.

However, if you have a need for more complex logic on the replacement, you can
pass a function instead of replacement text for the `repl` argument of
`re.sub`.

