When i changed two words in a string with other two words using re.sub i got the output. But when i tried that with numbers output is not coming correctly
>>> import re
>>> a='this is the string i want to change'
>>> re.sub('(.*)is(.*)want(.*)','\\1%s\\2%s\\3' %('was','wanted'),a)
'this was the string i wanted to change'
>>> re.sub('(.*)is(.*)want(.*)','\\1%s\\2%s\\3' %('was','12345'),a)
'this was\x8a345 to change'
>>>
i don’t know why this happens could u please tel me how to use this
Thanks in advance
What happens is that you’re passing in the replacement
r'\1was\212345\3', and Python cannot determine whether you want the backreference number 2, 21, 211, … . It just picks the largest one, 212345, which is obviously not a group index in your expression. Therefore, Python decides you meant the bytestring literalb'\212', which is a strange way of writing theb'\x8a'.To resolve the ambiguity, use the long backreference syntax,
\g<GROUP_NUMBER_HERE>: