2015-03-30 63 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