6

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

1 Answer 1

8

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 literal b'\212', which is a strange way of writing the b'\x8a'.

To resolve the ambiguity, use the long backreference syntax, \g<GROUP_NUMBER_HERE>:

>>> re.sub('(.*)is(.*)want(.*)','\\g<1>%s\\g<2>%s\\g<3>' %('was','12345'),a)
'this was the string i 12345 to change'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.