Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple substitutions of numbers in string using regex python

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

like image 293
Myjab Avatar asked Aug 14 '12 09:08

Myjab


1 Answers

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'
like image 155
phihag Avatar answered Oct 23 '22 18:10

phihag