Попробуйте отказаться от регулярок в пользу replace.
replace работает сильно быстрее.
str.replace() should be used whenever it's possible to. It's more explicit, simpler, and faster.
In [1]: import re
In [2]: text = """For python 2.5, 2.6, should I be using string.replace or re.sub for basic text replacements.
In PHP, this was explicitly stated but I can't find a similar note for python.
"""
In [3]: timeit text.replace('e', 'X')
1000000 loops, best of 3: 735 ns per loop
In [4]: timeit re.sub('e', 'X', text)
100000 loops, best of 3: 5.52 us per loop
https://stackoverflow.com/questions/5668947/use-py...