views:

120

answers:

8

How to replace the pattern in the string with

     decoded_str=" Name(++info++)Age(++info++)Adress of the emp(++info++)"

 The first pattern "(++info++)" needs to replaced with (++info a++)
 The second pattern "(++info++)" needs to replaced with (++info b++)
 The third pattern "(++info++)" needs to replaced with (++info c++)
 If there many more then it should be replaced accordingly
+1  A: 

Here's a quick hack to do it:

string=" Name(++info++)Age(++info++)Adress of the emp(++info++)"

def doit(s):
    import string
    allTheLetters = list(string.lowercase)
    i=0
    s2 = s.replace("++info++","++info "+allTheLetters[i]+"++",1)
    while (s2!=s):
        s=s2
        i=i+1
        s2 = s.replace("++info++","++info "+allTheLetters[i]+"++",1)
    return s

Note that performance is probably not very great.

phimuemue
+2  A: 

Here is a rather ugly yet pragmatic solution:

import string

decoded_str = " Name(++info++)Age(++info++)Adress of the emp(++info++)"
letters = list(string.lowercase)
token = "(++info++)"
rep_token = "(++info %s++)"

i = 0
while (token in decoded_str):
    decoded_str = decoded_str.replace(token, rep_token % letters[i], 1)
    i += 1

print decoded_str
Andrew Hare
+1 for well-named variables and readable code.
Bolo
Nice...............
Hulk
+3  A: 

This should be simple enough:

for character in range(ord('a'), ord('z')):
    if "(++info++)" not in decoded_str:
        break
    decoded_str = decoded_str.replace("(++info++)", "(++info {0}++)".format(chr(character)), 1)

print decoded_str

It has the added benefit of stopping at 'z'. If you want to wrap around:

import itertools

for character in itertools.cycle(range(ord('a'), ord('z'))):
    if "(++info++)" not in decoded_str:
        break
    decoded_str = decoded_str.replace("(++info++)", "(++info {0}++)".format(chr(character)), 1)

print decoded_str

And just for fun, a one-liner, and O(n):

dstr = "".join(x + "(++info {0}++)".format(chr(y)) for x, y in zip(dstr.split("(++info++)"), range(ord('a'), ord('z'))))[:-len("(++info a++)")]
carl
+1 Short and simple - nicely done!
Andrew Hare
+1 for `itertools.cycle`
Bolo
A: 
>>> import re
>>> rx = re.compile(r'\(\+\+info\+\+\)')
>>> s = "Name(++info++)Age(++info++)Adress of the emp(++info++)"
>>> atoz = iter("abcdefghijklmnopqrstuvwxyz")
>>> rx.sub(lambda m: '(++info ' + next(atoz) + '++)', s)
'Name(++info a++)Age(++info b++)Adress of the emp(++info c++)'
KennyTM
A: 
decoded_str=" Name(++info++)Age(++info++)Adress of the emp(++info++)"

import re
for i, f in enumerate(re.findall(r"\(\+\+info\+\+\)",decoded_str)):
    decoded_str = re.sub(r"\(\+\+info\+\+\)","(++info %s++)"%chr(97+i),decoded_str,1)
print decoded_str
ebt
+1  A: 
import re, string

decoded_str=" Name(++info++)Age(++info++)Adress of the emp(++info++)"

sub_func=('(++info %s++)'%c for c in '.'+string.ascii_lowercase).send
sub_func(None)
print re.sub('\(\+\+info\+\+\)', sub_func, decoded_str)
gnibbler
+2  A: 
import string

decoded_str = " Name(++info++)Age(++info++)Adress of the emp(++info++)"
s = decoded_str.replace('++info++', '++info %s++')
s % tuple(i for i in string.ascii_lowercase[:s.count('%s')])
killown
+1  A: 
from itertools import izip
import string
decoded_str=" Name(++info++)Age(++info++)Adress of the emp(++info++)"
parts = iter(decoded_str.split("(++info++)"))
first_part = next(parts)
tags = iter(string.ascii_lowercase)
encoded_str=first_part+"".join("(++info %s++)%s"%x for x in izip(tags, parts))
print encoded_str
gnibbler
@gnibbler Thanks for the example. It thought me one new thing and reminded me of about five I've forgotten.
aaronasterling