Python regex \number adding \0x

python, regex, replace

Solution

You need to start using raw strings (prefix the string with r):

>>> import re
>>> s = "num1 1 num2 5"
>>> re.sub(r"num1 (.*?) num2 (.*?)", r"1 \1 2 \2", s)
'1 1 2 5'

Otherwise you would need to escape your backslashes both for python and for the regex, like this:

>>> re.sub("num1 (.*?) num2 (.*?)", "1 \\1 2 \\2", s)
'1 1 2 5'

(this gets really old really fast, check out the opening paragraphs of the python regex docs

Problem

I am tring to go a simple regex replace on a string in python. This is my code: ``` >>> s = "num1 1 num2 5" >>> re.sub("num1 (.*?) num2 (.*?)","1 \1 2 \2",s) ``` I would expect an output like this, with the `\number`s being replaced with their corresponding groups. ``` '1 1 2 5' ``` However, this is the output I am getting: ``` '1 \x01 2 \x025' ``` And I'm kinda stumped as to why the `\x0`s are their, and not what I would like to be there. Many thanks for any help

Original source