我有一个乳胶文件,里面可能有这种代码
\MyTitle{This is a title with some \commands{with some arguments} inside}
which is followed by some text
with many lines and any kind of things inside
and at some point an abstract can be defined like
\MyAbstract{%
including the abstract which can have also \commands{inside with possiblguments} or not and can longer than a line
我要做的是编写一些python代码,在文件中找到标题和摘要。我所做的和正在做的是将文件作为字符串读取,并使用以下代码作为标题
^{pr2}$这一切都很好,直到我找到了一个标题或摘要,里面有其他大括号,这使得笨拙的regexp失败。如果我删除了ungreedy符号,那么它将匹配更多的文本,因为它都在一行中,我想匹配从原点大括号直到关闭它的那一个。在
最好的办法是什么?由于摘要可以跨越几行,而标题通常是一行,我不确定什么是最好的方法。在
我已经找过了,但找不到有效的解决办法。在
谢谢!在
如果胡子的嵌套只有1级,那么这个regex应该接近你想要的。
您可以测试它here
如果这不是Python re模块,而是支持递归的regex引擎
^{pr2}$(如PCRE或PyPi regex),那么类似的正则表达式可以工作:
相关问题 更多 >
编程相关推荐