2014-02-23 41 views
0

它嘗試從LaTeX文檔中獲取一些信息。不過,我試圖使用「preg_replace」將 http://regex101.com/r/kB7nD4 轉換爲php代碼,但出現了很多錯誤。PHP中的正則表達式(LaTeX-itemnize)

正則表達式是

\\item(([\s\S]*)^[^\\item]+\\(?!\\item)) 

,並應提取乳膠列表項列舉環境。但

$newlatex = preg_replace('/\\\\item(([\\s\\S]*)^[^\\\\item]+\\\\(?!\\\\item))/m','<li>$1</li>',$oldlatex); 

有人可以給我一個建議嗎?這個表達能夠簡化嗎?

回答

1

我跑這個代碼沒有問題

<?php 
$string='Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. 

\begin{itemnize} 
\item first outer 
     first outer second line 
    \begin{itemnize} 
    \item first inner first line with $x^2=y$ 
      first inner second line 
    \item second inner 
    \end{itemnize} 
    \item second out 
\end{itemnize} 


Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dol'; 
// 
$newlatex = preg_replace('/\\\\item(([\\s\\S]*)^[^\\\\item]+\\\\(?!\\\\item))/m','<li>$1</li>',$string); 
print($string); 
print($newlatex); 

產生這樣的結果

\begin{itemnize} 
<li> first outer 
     first outer second line 
    \begin{itemnize} 
    \item first inner first line with $x^2=y$ 
      first inner second line 
    \item second inner 
    \end{itemnize} 
    \</li>item second out 
\end{itemnize} 

沒有明顯的錯誤 你能給你的錯誤的詳細信息?

+0

在我的本地系統上$ newlatex是一個空字符串。我認爲語法錯了。不管怎樣,謝謝你。 – wieschoo