删除 $ 之间的所有内容,包括乳胶文件中的多行



我正在处理 laTex 文件,我需要删除两个 $ 之间的所有内容,包括换行符,只保留英文文本。

我正在使用这样的命令来处理文件:

find "." -name "*.tex" | xargs perl -pi -e 's/$[^$].*?$/ /g' *

例:

Then use the naturality formula 
$t_{G^{n-1}M} G^{i+1} (epsilon_{G^{n-i}M}) 
= G^{i+1} (epsilon_{G^{n-i}M}) t_{G^n M}$ on the left-hand side.

输出:

Then use the naturality formula 
on the left-hand side.

文件中的另一个示例:

例:

begin{itemize}
item $M$ is atomic and finitely generated;
item $M$ is cancellative;
item $(M, le_L)$ and $(M, le_R)$ are lattices;
item there exists an element $Delta in M$, called {it Garside element}, such that the set 
$L(Delta)= { x in M; xle_L Delta}$ generates $M$ and is equal to $R(Delta)= { xin M; 
xle_R Delta}$.
end{itemize}

输出:

begin{itemize}
item   is atomic and finitely generated;
item   is cancellative;
item   and   are lattices;
item there exists an element  , called {it Garside element}, such that the set 
generates   and is equal to $R(Delta)= { xin M; 
xle_R Delta}$.
end{itemize} 

如果你能注意到 ( $R(\Delta)= { x\in M; x\le_R \Delta}$.) 无法删除!!

示例 2 来自不同的文件,输入与输出相同没有任何变化:

Using the fact that   is atomic and that $L(Delta)= 
{x in M; x le_L Delta} M pi_L(a) neq 1 a neq 
1 k partial_L^k(a)=1 k$ be the

我猜当它应该匹配的文本跨越多行时,这不匹配。

您有[^$].*?匹配一个未使用[^$]$的字符,然后匹配.*?与任何不是换行符的字符零次或多次懒惰匹配。这适用于您的单行情况,因为惰性修饰符试图在另一个.之前匹配一个$,但多行情况失败.因为不匹配换行符。

正确和更有效的将是[^$]*匹配尽可能多的非$字符,包括换行符。

所以你的命令将是

s/$[^$]*$/ /g

或者在我看来看起来更干净,使用非标准分隔符并避免"栅栏"外观/

s~$[^$]*$~ ~g

演示

Perl 正在逐行处理您的文件,这是换行符匹配失败的另一个原因。在SO上已经有许多关于这个问题的书面答案,并且是由比我更了解perl的人编写的:如何在perl中匹配多行数据

最新更新