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
>>> 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).