我想使用pythonregexp删除LaTeX文件中的注释。在LaTeX中,注释以“%”开头。但是如果%字符被转义(“\%”),那么它不是注释,而是符号percent。在
这个任务只是我在我的LaTeX文本上应用的众多regexp中的一个。我把所有这些reg exp存储在dicts列表中。在
我面临的问题是,我用于修剪注释的regexp不起作用(因为我不知道如何指定字符集“not backslash”)。字符集中的反斜杠转义结束的']',并且regexp不正确。在
我的代码:regexps=[]
regexps.append({r'left':'%.*', 'right':r''}) # this strips all the comments, but messes up with the percent characters (\%)
regexps.append({r'left':'[^\]%.*', 'right':r''}) # this is incorrect (escapes the closing "]" )
return applyRegexps(latexText, regexps)
def applyRegexps(text, listRegExp):
""" Applies successively many regexps to a text"""
if testMode:
print str(listRegExp)
# apply all the regexps in the list
for element in listRegExp:
left = element['left']
right = element['right']
r=re.compile(left)
text=r.sub(right,text)
return text
任何帮助都将不胜感激。谢谢!在
吉尔