Q:

How to replace special regex chars in Python?

I need to match the following pattern:
text1.cps.text2
text1.text2.cps.text3
cps.text1

where text1, text2 are some random words. cps is one of several possible words. So I wrote the following regexp:
pattern = "(\w+\.)*?({0})(\w+\.)*"

And than I iterate over list of possible words:
for word in ['cps.','cps.tbl.']:
   p = pattern.format(word)

So the problem is that item from the possible words can contain special regex chars. So I am looking for function that replaces each special character into something like that:
cps\. and cps\.tbl\.

A:

The re module defines an escape function which does just that. Something like:
for word in words:
    pattern = re.escape(word)
    ...

