0

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\.
ig-melnyk
  • 2,679
  • 2
  • 20
  • 33

1 Answers1

1

The re module defines an escape function which does just that. Something like:

for word in words:
    pattern = re.escape(word)
    ...
ig0774
  • 36,969
  • 3
  • 54
  • 56