[英]REGEX parsing commands from latex lines - Python
我正在嘗試從加載的每一行解析並刪除任何\\command
( \\textit
等...)(來自.tex文件或來自lilypond文件的其他命令為[\\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
您可以使用此模式使用簡單的正則表達式替換^\\\\[^\\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
這將有效:
'\\\w+\s'
它搜索反斜杠,然后搜索一個或多個字符和空格。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.