Loading

正则表达式 知乎Tex公式提取

import re
import urllib.parse

# https://learnbyexample.github.io/py_regular_expressions/groupings-and-backreferences.html
s = input("input string for formula conversion\n")
s = urllib.parse.unquote(s)

p_1 = "([\+]*)\+"
repl_1 = "\g<1> "

# s = re.sub(repr(p_1), repr(repl_1), s)
s = re.sub(p_1, repl_1, s)

p_2 = "\+\+"
repl_2 = "\+"
s = re.sub(p_2, repl_2, s)

print(s)

input("press a key to terminate")
posted @ 2021-10-25 11:02  ZXYFrank  阅读(126)  评论(0编辑  收藏  举报