String manipulation using RE

aditya shukla adityashukla1983 at
Mon Oct 27 01:56:21 CET 2008

Hello folks, i have a string



now i have to convert this string to

So i used the logic eg. taking the substring "1):3" and converting it to
"1):2"(3-1=2) so on for all the similar substrings.But i am not able to
replace them back into the original string.
This is what my code looks like

import re

str = "(((A:1,B:1):3,C:3):4,((E:1,F:1):2,D:2):4)"

p =re.compile('\d\):\d') # compiling an re object

list1 =[]

iterator = p.finditer(str)

for match in iterator:

       x =

       l = x.split('):')

       value = int(l[1])-int(l[0])

       z= repr(value)

       rm = l[1]

       x= x.rstrip(rm)

       y = x + repr(value)

      print y

I am able to get the desired output further, any help is appreciated


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list