我正在尝试从加载的每一行(从 .tex 文件或来自 lilypond 文件的其他命令作为 )解析和删除任何\command
(\textit
等... [\clef, \key, \time]
)。
我怎么能那样做?
我试过的
import re
f = open('example.tex')
lines = f.readlines()
f.close()
pattern = '^\\*([a-z]|[0-9])' # this is the wrong regex!!
clean = []
for line in lines:
remove = re.match(pattern, line)
if remove:
clean.append(remove.group())
print(clean)
例子
输入
#!/usr/bin/latex
\item More things
\subitem Anything
预期产出
More things
Anything