2017-10-05 86 views
0

我正在使用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; x\le_L \Delta\}$ generates $M$ and is equal to $R(\Delta)= \{ x\in M; 
x\le_R \Delta\}$. 
\end{itemize} 

OUTPUT:

\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)= \{ x\in M; 
x\le_R \Delta\}$. 
\end{itemize} 

如果你可以看到($ R(\德爾塔)= {在一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 
+0

*「它可以工作,但不適用於我在文件中的所有情況」*請給出一些示例不能正常工作的情況以及要處理的簡要示例文檔。要求我們在沒有看到任何輸入的情況下編寫軟件是不公平的。 – Borodin

+0

我編輯回你的正則表達式/命令作爲我相關的信息的問題。我也更新了我的回答 –

回答

0

我猜測這是不匹配時,它應當匹配文本跨越多行。

你有[^\$].*?其中一個字符不$使用[^\$]然後匹配.*?這不是一個換行符懶洋洋的零次或多次匹配任何字符相匹配。這適用於您的單行案例,因爲懶惰修改器試圖在另一個.之前匹配$,但由於.與換行符不匹配,所以多行案例失敗。

正確和更有效的將是[^\$]*它將盡可能多地匹配包括換行符在內的非$字符。

所以你的命令將是

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

或清潔期待在我看來,使用非標準的分隔符,避免 '柵欄柱' 看/\

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

Demo

的Perl一行一行地處理你的文件,這是跨越換行失敗的另一個原因。對於這個問題已經有很多文檔的答案,並且由知道Perl比我更好的人編寫:How to match multiline data in perl

+0

不幸的是它沒有工作!比如這個案子還沒有解決,..投影$ I_ {d,i} \ rightarrow M_ {d,i} M_ {d,0} $,這裏所有的.. – Zain

+0

這個在我的測試中有效。 。 。 https://regex101.com/r/qZvkQ3/2 –

+1

你能否更新你的問題與多個案例不工作的多個例子? –