Right-to-left string replace in Python?

后端 未结 7 707
轻奢々
轻奢々 2020-12-01 10:20

I want to do a string replace in Python, but only do the first instance going from right to left. In an ideal world I\'d have:

myStr = \"mississippi\"
print          


        
相关标签:
7条回答
  • 2020-12-01 10:32
    def rreplace(s, old, new):
        try:
            place = s.rindex(old)
            return ''.join((s[:place],new,s[place+len(old):]))
        except ValueError:
            return s
    
    0 讨论(0)
  • 2020-12-01 10:44

    rsplit and join could be used to simulate the effects of an rreplace

    >>> 'XXX'.join('mississippi'.rsplit('iss', 1))
    'missXXXippi'
    
    0 讨论(0)
  • 2020-12-01 10:44

    you may reverse a string like so:

    myStr[::-1]
    

    to replace just add the .replace:

    print myStr[::-1].replace("iss","XXX",1)
    

    however now your string is backwards, so re-reverse it:

    myStr[::-1].replace("iss","XXX",1)[::-1]
    

    and you're done. If your replace strings are static just reverse them in file to reduce overhead. If not, the same trick will work.

    myStr[::-1].replace("iss"[::-1],"XXX"[::-1],1)[::-1]
    
    0 讨论(0)
  • 2020-12-01 10:45

    It's kind of a dirty hack, but you could reverse the string and replace with also reversed strings.

    "mississippi".reverse().replace('iss'.reverse(), 'XXX'.reverse(),1).reverse()
    
    0 讨论(0)
  • 2020-12-01 10:48

    You could also use str.rpartition() which splits the string by the specified separator from right and returns a tuple:

    myStr = "mississippi"
    
    first, sep, last = myStr.rpartition('iss')
    print(first + 'XXX' + last)
    # missXXXippi
    
    0 讨论(0)
  • 2020-12-01 10:50
    >>> re.sub(r'(.*)iss',r'\1XXX',myStr)
    'missXXXippi'
    

    The regex engine cosumes all the string and then starts backtracking untill iss is found. Then it replaces the found string with the needed pattern.


    Some speed tests

    The solution with [::-1] turns out to be faster.

    The solution with re was only faster for long strings (longer than 1 million symbols).

    0 讨论(0)
提交回复
热议问题