python - Character encoding in regular-expression substitution -
i trying substitute regular expression pattern. more want replace $$ some_latex $$
$latex some_latex$
. tried following.
in [22]: re.sub(r'\$\$(?p<pat>.+?)\$\$', r'$latex \1 $', "$$ x = \frac{2}{3}$$", re.dotall | re.u) out[22]: '$latex x = \x0crac{2}{3} $'
the word \frac
converted \x0crac
. how overcome this. tried following also. didn't help.
re.sub(r'\$\$(?p<pat>.+?)\$\$', r'$latex \1 $', "$$ x = \frac{2{3}$$".encode("string_escape"), re.dotall | re.u) '$latex x = \\x0crac{2}{3} $'
this has nothing regular expression; \f
form-feed escape code:
>>> '\f' '\x0c' >>> len('\f') 1
the character already present in input, before replacements take place:
>>> "$$ x = \frac{2}{3}$$" '$$ x = \x0crac{2}{3}$$'
double slash or use raw string literal:
>>> '\\f' '\\f' >>> r'\f' '\\f' >>> print '\\f' \f >>> len('\\f') 2
Comments
Post a Comment