I have the following text.
"\*hello* * . [ }"
It should be escaped like this:
"\*hello\\* \* \\. \\[ \\}"
How to do this with python regex?
Every special character (the special characters are: _
, *
, [
, ]
, (
, )
, ~
, `
, >
, #
, +
, -
, =
, |
, {
, }
, .
, !
must be escaped with the preceding character \
.
I tried it with this but then every character is escaped:
escape_chars = r'_*[]()~`>#+-=|{}.!'
return re.sub(f'([{re.escape(escape_chars)}])', r'\\\1', text)
Then the text is unformatted like this:
\*hello\* \* \. \[ \}
But it should be like this:
**hello** \* \. \[ \}
Some examples:
At \* \* \*
only the middle one should be escaped At \{ \{ \}
only the middle one should be escaped
I need this for tex formatting: https://core.telegram.org/bots/api#markdownv2-style