2015-03-30 98 views
-2

示例代碼:使用Python正則表達式來簡化乳膠形態的影響乳膠

%Chebyshev of second kind 
\begin{equation} 
\sum_{\ell \hiderel{=} 0}^n\frac{(\ChebyU{\ell}@{x} )^2}*{ \frac{\pi}{2} }*=\frac{ 2^n }{ \frac{\pi}{2} 2^n+1 }\frac{ \ChebyU{n+1}@{x} \ChebyU{n}@{y} - \ChebyU{n}@{x} \ChebyU{n+1}@{y} }{x-y} 
\end{equation} 

\frac{(\ChebyU{\ell}@{x})^2}*{\frac{\pi}{2}}是我期待在這個特定的情況下分數。由於它是另一個分數的分母的一部分,我想用Python正則表達式將其從a/(b/c)更改爲(ac)/b

輸出示例:

%Chebyshev of second kind 
\begin{equation} 
\sum_{\ell \hiderel{=} 0}^n\frac{(2 \ChebyU{\ell}@{x} )^2}{\pi}*=\frac{ 2^n }{ \frac{\pi}{2} 2^n+1 }\frac{ \ChebyU{n+1}@{x} \ChebyU{n}@{y} - \ChebyU{n}@{x} \ChebyU{n+1}@{y} }{x-y} 
\end{equation} 

最終結果:\frac{(2\ChebyU{\ell}@{x})^2}{\pi}是,正則表達式應該導致部分

我怎麼會去用Python與正則表達式這樣做?

回答

1

這是一個正常工作的正則表達式。請注意,我對LaTeX表達式進行了更改,因爲我認爲存在錯誤(*符號)。

astr = '\frac{(\ChebyU{\ell}@{x})^2}{\frac{\pi}{2}}' 

import re 
pat = re.compile('\\frac\{(.*?)\}\{\\frac\{(.*?)\}\{(.*?)\}\}') 
match = pat.match(astr) 
if match: 
    expr = '\\frac{{{0}*{2}}}{{{1}}}'.format(*match.groups()) 
    print expr 

注意:我沒有在原始表達式中包含空格。

+0

對於遲到的回覆感到抱歉,但是謝謝你的工作。 – zara 2015-06-15 15:19:59

+0

不客氣:-) – 2015-06-15 15:26:11