in reply to Optimizing a regex that replaces near the end of a string

This might not apply in your specific case, but perhaps a nice idea anyway:

Suppose you know an exact substring that occurs before the match. Then you can find it's position with rindex, set pos to that value, and add \G.* in front of your regex.