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

You should realize that 1) Perl regexes aren't very fast, and 2) the optimizer is really awesome. Perl regexes look fast because of the optimizer.

If you have a string like that, and you need to replace something near </body>, I presume your pattern has </body in it. It might very well be that the optimizer spots this and does all the optimization you want already.

Try running it with regex debugging enabled (if you have 5.10).