开发者

How to replace a suffix of a string?

I know how to make string replacements in Python but I need a way to replace only if the sequence is located at the end of the word.开发者_开发技巧

For example:

rule: at -> ate
so:
cat -> cate
but:
attorney -> attorney


There is no special method to do this, but it's quite simple anyways:

w = 'at'
repl = 'ate'
s = 'cat'

if s.endswith(w):
    # if s ends with w, take only the part before w and add the replacement
    s = s[:-len(w)] + repl


A regex can do that with easyness:

import re

regx = re.compile('at\\b')

ch = 'the fat cat was impressed by all the rats gathering at one corner of the great room'

print ch
print
print regx.sub('ATU',ch)

result

the fat cat was impressed by all the rats gathering at one corner of the great room

the fATU cATU was impressed by all the rats gathering ATU one corner of the greATU room

.

PS

With regexes, we can perform very complicated tasks.

For exemple, replacing several kinds of strings with a particular replacement for each, thanks to the use of a callback function (here named repl, that receives catched MatchObjects )

import re

regx = re.compile('(at\\b)|([^ ]r(?! ))')

def repl(mat, dic = {1:'ATU',2:'XIXI'}):
    return dic[mat.lastindex]

ch = 'the fat cat was impressed by all the rats gathering at one corner of the great room'

print ch
print
print regx.sub(repl,ch)

result

the fat cat was impressed by all the rats gathering at one corner of the great room

the fATU cATU was imXIXIessed by all the rats gathXIXIing ATU one cXIXIner of the XIXIeATU room


You could use regular expressions with the re module and the following code:

re.sub(re.escape(suffix)+"$", replacement, word)

If you need to do this for text longer than a single word

re.sub(re.escape(suffix)+r"\b", replacement, word)

because \b is a word boundary, so a suffix followed by a word boundary is at the end of any word in the text

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜