0

I have a test.ini file with the following content:

[search]
pattern = ABC\d{2}-\d{3} 

I need to use this pattern in a regular expression. Therefore, I want to use configparser to read the .ini file

import configparser
settings = configparser.ConfigParser()
settings.read("test.ini")
pattern = settings["search"]["pattern"]

but this does not give me the content regular expression string. It encodes \ as \\.

pattern --> 'ABC\\d{2}-\\d{3}'

I want to replace the \\ with \

However:

pattern.replace("\\\\","\\") 

does not work

user7431005
  • 3,899
  • 4
  • 22
  • 49
  • 2
    Are you absolutely sure `pattern` does not contain a single backslash? Remember that a `repr` string *doubles* backslashes. Are you getting your result when printing the *string* or its *representation*? – Jongware Mar 31 '20 at 15:59
  • that's it. the repr got me confused. Should have simply tried it instead of trying to fix it – user7431005 Mar 31 '20 at 16:01

0 Answers0