I am proposing in this issue a patch to improve the algorithm to replace text with exceptions. I have found that the current algorithm process the exception regexes many times, and this can be avoided. In my tests, with my patch in long text pages I obtain significant performance improvements.
I understand this patch touches a critical part of the pywikibot code, so unit tests should be provided, but I am Python newbie and I am afraid that testing in Python is a little out of my knowledge without a bit of guidance.
I hope you could test the patch and confirm my results.