我有一个 LaTeX 文档,看起来像这样:
Now, we go onto the following theorem:
\theorem{$\sqrt{2}$ is irrational.}{Blah blah blah...}
\remark{This proof is often attributed to Hippasus of Metapontum.}
We can prove a more general result:
\theorem{For $n$ a non-square integer, $\sqrt{n}$ is irrational.}{Blah blah blah...}
我想匹配所有看起来像的东西\theoremnoname{Something 1}{Something 2}
,特别是获取零件Something 1
和Something 2
作为匹配的不同部分。
困难在于零件中的{
s 和}
s 数量未知,并试图使它们匹配。
我已经尝试过正则表达式\theorem\{([^{}]+)\}(\{([^{}]*(\{[^{}]*\}[^{}]*)*)\})
(即使部分变得贪婪),但它没有返回任何匹配项。
如果您可以假设该模式
}{
没有出现在第一组内,则正则表达式\\theorem\{(.*)\}\{(.*)\}
应该可以工作。我认为您的正则表达式前缀
\theorem\{([^{}]+)\}...
是错误的,这确实不会匹配任何内容,因为\t
尝试匹配选项卡(因此请使用\\
)。但即使在那之后,您的正则表达式也只会到达第一个开头{
,因为[^{}]
仅匹配与{
和不同的字符}
,并且无法将找到的开头{
与结尾匹配}
。我可以简单地使用 grep 并将这些部分放入两个文件中,如下所示:
这将为您提供示例文本的以下内容: