我有一个Latex文件,其中许多文本都带有\red{}
标记,但在\red{}
内部也可能有括号,例如:\red{这里是\underline{下划线}文本}
。我想要去掉红色并进行一些搜索后,我写了这个Python脚本:
import os, re, sys
#Start program in terminal with
#python RedRemover.py filename
#sys.argv[1] then has the value filename
ifn = sys.argv[1]
#Open file and read it
f = open(ifn, "r")
c = f.read()
#The whole file content is now stored in the string c
#Remove occurences of \red{...} in c
c=re.sub(r'\\red\{(?:[^\}|]*\|)?([^\}|]*)\}', r'\1', c)
#Write c into new file
Nf=open("RedRemoved_"+ifn,"w")
Nf.write(c)
f.close()
Nf.close()
但是这将会把
\red{这里有\underline{下划线}文本}
转换成
这里有\underline{下划线文本}
这不是我想要的。我想要的是
这里有\underline{下划线}文本