I have a JSON file where I store a mapping, which contains regexes, like the ones below:
"F(\\d)": "field-\\\\1",
"FLR[ ]*(\\w)": "floor-\\\\1",
To comply with the standard I escape the backslashes, the actually regexps should contain \d
, \w
, and \\1
.
Once I read this JSON with json.load() I still need to post-process the resulting dictionary to get correct regexps. I need to substitute a \\
with \
. What's the best way to this?
So far I tried both re.sub()
and str.replace()
and in both cases it's not clear how to represent a single backslash in substation.
For example, I don't understand why the following doesn't produce a single backslash:
In [76]: "\\\\d".replace("\\\\", "\\")
Out[76]: '\\d'