How to replace a suffix of a string?

Question:

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
Asked By: tomermes

||

Answers:

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
Answered By: Jochen Ritzel

Is there a certain length the suffix has to be? If not, can you just start at index -1 of the string and work backwards the number of characters you are searching for? Then just perform the string replacement like you would normally

Answered By: Mike Caputo

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

Answered By: murgatroid99

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
Answered By: eyquem
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.