-2

Sample code in Latex:

%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}} is the fraction I am looking at for this specific case. Since it is a fraction in the denominator of another fraction I would like to use Python regex to change this from a/(b/c) to (ac)/b.

Example output:

%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}

End result: \frac{(2\ChebyU{\ell}@{x})^2}{\pi} is the fraction that the regex should result in.

How would I go about doing this with regex in Python?

Celeo
  • 5,583
  • 8
  • 39
  • 41
zara
  • 375
  • 1
  • 4
  • 18

1 Answers1

1

Here is a regex that should work. Note that I made a change in your LaTeX expression because I think there was an error (* sign).

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

NB: I did not include the spaces in your original expression.

Julien Spronck
  • 15,069
  • 4
  • 47
  • 55