开发者

Replace a pattern in python

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 mor开发者_如何转开发e then it should be replaced accordingly


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++)")]


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')])


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


>>> 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++)'


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.


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)


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜