4

我正在尝试从加载的每一行(从 .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
4

2 回答 2

2

您可以使用此模式 ^\\[^\s]*使用简单的正则表达式替换:

python中的示例代码:

import re
p = re.compile(r"^\\[^\s]*", re.MULTILINE)

str = '''
\item More things
\subitem Anything
'''

subst = ""

print re.sub(p, subst, str)

结果将是:

More things
Anything
于 2014-05-05T22:24:50.347 回答
0

这将起作用:

'\\\w+\s'

它搜索反斜杠,然后搜索一个或多个字符和一个空格。

于 2014-05-05T22:15:23.480 回答