简体   繁体   中英

multiline Python regex expression

I am trying to remove the sections of code between \\\\[-16pt] and \\\\ \\\\thinhline by using regex.

This is the regex expression I created that works for everything of that format except for the content I need to be matched:

pattern = re.compile(r"""(\\\[-16pt]\n)    # Start. Don't technically need to capture.
                             (.*?)             # What we want. Must capture ;)
                             (\n\\\n\thinhline) # End. Also don't really need to capture
                      """, re.X | re.DOTALL)

for m in re.finditer(pattern,content):
    print("Matched:\n----\n%s\n----\n" % m.group(2))

Here is the content I will be testing:

I am just trying to separate out the content using this pattern but after testing it on pythex I realized that my pattern is not working. How can I fix this pattern so the above content is correctly matched?

Change your regex like below,

r'(?s)(\\\[-16pt]\n)(.*?)(\n *\\\\\n\\thinhline)'

To match literal \\t , your pattern must be \\\\t . Get your string from group index 2.

OR

Use lookarounds instead of capturing groups.

r'(?s)(?<=\\\[-16pt]\n).*?(?=\n *\\\\\n\\thinhline)'

Get your string from index 0.

>>> s = r"""\multicolumn{1}{c}{$k_n$}&
\multicolumn{1}{c}{$\ifrac{\tilde{k}_n}{k_n}$}&
\multicolumn{1}{c}{Constraints}\\
\thinhline
\\[-16pt]
  Jacobi
  & $\JacobiP{\alpha}{\beta}{n}@{x}$
    \MarkDefn[P z 3 - jacobi]{$\JacobiP{\alpha}{\beta}{n}@{x}$}{Jacobi polynomial}%
  & $(-1,1)$
  & $(1 - x)^{\alpha} (1 + x)^{\beta}$
  & $\begin{cases} \ifrac{2^{\alpha+\beta+1}\EulerGamma@{\alpha+1}\EulerGamma@{\beta+1}}{\EulerGamma@{\alpha+\beta+2}}, &\text{$n = 0$} \end{cases}$
  & $\begin{cases} \ifrac{2^{\alpha+\beta+1}\EulerGamma@{\alpha+1}\EulerGamma@{\beta+1}}{\EulerGamma@{\alpha+\beta+2}}, & \text{$n = 0$}\end{cases}$
  & $\dfrac{\pochhammer{n+\alpha+\beta+1}{n}}{2^n n!}$
  & $\dfrac{n (\alpha-\beta)}{2n+\alpha+\beta}$
  & $\alpha,\beta > -1$

  \\
\thinhline
\\[-16pt]
  \begin{minipage}[c]{1.0in}\centering Ultraspherical\\(Gegenbauer)\end{minipage}
  & $\Ultraspherical{\lambda}{n}@{x}$
    \MarkDefn[C z 3 + ultraspherical]{$\Ultraspherical{\lambda}{n}@{x}$}{ultraspherical (or Gegenbauer) polynomial}%
  & $(-1,1)$
  & $(1 - x^2)^{\lambda-\frac{1}{2}}$
  & $\dfrac{2^{1-2\lambda} \pi \EulerGamma@{n+2\lambda}}
           {(n+\lambda) \left( \EulerGamma@{\lambda} \right)^2 n!}$
  & $\dfrac{2^n \pochhammer{\lambda}{n}}{n!}$ & $0$
  & $\lambda > -\tfrac{1}{2}, \lambda \ne 0 $
\\
\thinhline
\\[-16pt]
  \begin{minipage}[c]{1.0in}\centering Chebyshev\\ of first kind\end{minipage}
  & $\ChebyT{n}@{x}$
    \MarkDefn[T z 1 + chebyshev]{$\ChebyT{n}@{x}$}{Chebyshev polynomial of the first kind}%
  & $(-1,1)$ & $(1 - x^2)^{-\frac{1}{2}}$
  & $\begin{cases} \tfrac{1}{2}\pi, &\text{$n>0$} \\ \pi, &\text{$n = 0$} \end{cases}$
  & $\begin{cases} 2^{n-1}, & \text{$n > 0$} \\ 1, & \text{$n = 0$}\end{cases}$
  & $0$
  &
\\
\thinhline
\\[-16pt]
  \begin{minipage}[c]{1.0in}\centering Chebyshev\\of second kind\end{minipage}
  & $\ChebyU{n}@{x}$
    \MarkDefn[U z 1 + chebyshev]{$\ChebyU{n}@{x}$}{Chebyshev polynomial of the second kind}%
  & $(-1,1)$ & $(1 - x^2)^{\frac{1}{2}}$
  & $\tfrac{1}{2} \pi$
  & $2^n$
  & $0$
  &
\\
\thinhline
\\[-16pt]
  \begin{minipage}[c]{1.0in}\centering Chebyshev\\of third kind\end{minipage}
  & $\ChebyV{n}@{x}$
    \MarkDefn[V z 1 + chebyshev]{$\ChebyV{n}@{x}$}{Chebyshev polynomial of the third kind}%
  & $(-1,1)$ & $(1 - x)^{\frac{1}{2}} (1 + x)^{-\frac{1}{2}}$
  & $\pi$ & $2^n$
  & $\tfrac{1}{2}$
  &
\\
\thinhline
\\[-16pt]
  \begin{minipage}[c]{1.0in}\centering Chebyshev\\of fourth kind\end{minipage}
i\[-16pt]
  & $\ChebyW{n}@{x}$
    \MarkDefn[W z 1 + chebyshev]{$\ChebyW{n}@{x}$}{Chebyshev polynomial of the fourth kind}%
  & $(-1,1)$ & $(1 - x)^{-\frac{1}{2}} (1 + x)^{\frac{1}{2}}$
  & $\pi$
  & $-\tfrac{1}{2}$
\\
\thinhline
\\[-16pt]
  \begin{minipage}[c]{1.2in}\centering Shifted Chebyshev\\of first kind\end{minipage}
  & $\ChebyTs{n}@{x}$
    \MarkDefn[T z 3 + chebyshev]{$\ChebyTs{n}@{x}$}{shifted Chebyshev polynomial of the first kind}%
  & $(0,1)$
  & $(x - x^2)^{-\frac{1}{2}}$
  & $\begin{cases} \tfrac{1}{2} \pi, &\text{$n > 0$}
    \\ \pi, &\text{$n = 0$} \end{cases}$
  & $\begin{cases} 2^{2n-1}, &\text{$n > 0$} \\ 1, &\text{$n = 0$} \end{cases}$
  & $-\tfrac{1}{2} n$
  &
\\
\thinhline
\\[-16pt]
  \begin{minipage}[c]{1.2in}\centering Shifted Chebyshev\\of second kind\end{minipage}
  & $\ChebyUs{n}@{x}$
    \MarkDefn[U z 3 + chebyshev]{$\ChebyUs{n}@{x}$}{shifted Chebyshev polynomial of the second kind}%
  & $(0,1)$ & $(x - x^2)^{\frac{1}{2}}$
  & $\tfrac{1}{8} \pi$
  & $2^{2n}$
  & $-\tfrac{1}{2}n$
  &
\\
\thinhline
\\[-16pt]
  Legendre
  & $\LegendrePoly{n}@{x}$
    \MarkDefn[P z 1 + legendre]{$\LegendrePoly{n}@{x}$}{Legendre polynomial}%
  & $(-1,1)$ & $1$
  & $\ifrac{2}{(2n+1)}$
  & $\ifrac{2^n \pochhammer{\frac{1}{2}}{n}}{n!}$
  & $0$
  &
\\
\thinhline
\\[-16pt]
  Laguerre
  & $\LaguerreL[\alpha]{n}@{x}$
   \MarkDefn[L z 3 + laguerre]{$\LaguerreL[\alpha]{n}@{x}$}{Laguerre (or generalized Laguerre) polynomial}%
   \MarkNotation[L z 1 + laguerre]{$\LaguerreL{n}@{x}$}{Laguerre polynomial}%
  & $(0,\infty)$
  & $e^{-x} x^{\alpha}$
  & $\ifrac{\EulerGamma@{n+\alpha+1}}{n!}$
  & $\ifrac{\opminus^n}{n!}$
  & $-n (n+\alpha)$
  & $\alpha > -1$
\\
\thinhline
\\[-16pt]
  Hermite
  & $\HermiteH{n}@{x}$
   \MarkDefn[H z 1 + hermite]{$\HermiteH{n}@{x}$}{Hermite polynomial}%
  & $(-\infty,\infty)$
  & $e^{-x^2}$
  & $\pi^{\frac{1}{2}} 2^n n!$
  & $2^n$
  & $0$
  &
\\
\thinhline
\\[-16pt]
  Hermite
  & $\HermiteHe{n}@{x}$
   \MarkDefn[H z 1 - hermite]{$\HermiteHe{n}@{x}$}{Hermite polynomial}%
  & $(-\infty,\infty)$
  & $e^{-\frac{1}{2} x^2}$
  & $(2\pi)^{\frac{1}{2}} n!$
  & $1$
  & $0$ & \\
\hline
\end{tabular}
\end{table}
\end{landscape}
%
\end{onecolumn*}

For exact values of the coefficients of the Jacobi polynomials
\index{Chebyshev polynomials!tables!of coefficients}%
\index{classical orthogonal polynomials!tables!of coefficients}%
\index{Hermite polynomials!tables!of coefficients}%
\index{Jacobi polynomials!tables of coefficients}%
\index{Laguerre polynomials!tables!of coefficients}%
\index{Legendre polynomials!tables!of coefficients}%
\index{ultraspherical polynomials!tables of coefficients}%
$\JacobiP{\alpha}{\beta}{n}@{x}$, the ultraspherical polynomials
$\Ultraspherical{\lambda}{n}@{x}$, the Chebyshev polynomials $\ChebyT{n}@{x}$
and $\ChebyU{n}@{x}$, the Legendre polynomials $\LegendrePoly{n}@{x}$, the
Laguerre polynomials $\LaguerreL{n}@{x}$, and the Hermite polynomials
$\HermiteH{n}@{x}$, see \citet[pp.~793--801]{Abramowitz:1964:HMF}. The Jacobi
polynomials are in powers of $x-1$ for $n = 0,1,\dots,6$. The ultraspherical
polynomials are in powers of $x$ for $n = 0,1,\dots,6$. The other polynomials
are in powers of $x$ for $n = 0,1,\dots,12$. See also \S\ref{sec:OP.CP.RE.Coeff}."""
>>> for m in re.finditer(r'(?s)(\\\[-16pt]\n)(.*?)(\n *\\\\\n\\thinhline)', s):
        print("Matched:\n----\n%s\n----\n" % m.group(2))


Matched:
----
  Jacobi
  & $\JacobiP{\alpha}{\beta}{n}@{x}$
    \MarkDefn[P z 3 - jacobi]{$\JacobiP{\alpha}{\beta}{n}@{x}$}{Jacobi polynomial}%
  & $(-1,1)$
  & $(1 - x)^{\alpha} (1 + x)^{\beta}$
  & $\begin{cases} \ifrac{2^{\alpha+\beta+1}\EulerGamma@{\alpha+1}\EulerGamma@{\beta+1}}{\EulerGamma@{\alpha+\beta+2}}, &\text{$n = 0$} \end{cases}$
  & $\begin{cases} \ifrac{2^{\alpha+\beta+1}\EulerGamma@{\alpha+1}\EulerGamma@{\beta+1}}{\EulerGamma@{\alpha+\beta+2}}, & \text{$n = 0$}\end{cases}$
  & $\dfrac{\pochhammer{n+\alpha+\beta+1}{n}}{2^n n!}$
  & $\dfrac{n (\alpha-\beta)}{2n+\alpha+\beta}$
  & $\alpha,\beta > -1$

----

Matched:
----
  \begin{minipage}[c]{1.0in}\centering Ultraspherical\\(Gegenbauer)\end{minipage}
  & $\Ultraspherical{\lambda}{n}@{x}$
    \MarkDefn[C z 3 + ultraspherical]{$\Ultraspherical{\lambda}{n}@{x}$}{ultraspherical (or Gegenbauer) polynomial}%
  & $(-1,1)$
  & $(1 - x^2)^{\lambda-\frac{1}{2}}$
  & $\dfrac{2^{1-2\lambda} \pi \EulerGamma@{n+2\lambda}}
           {(n+\lambda) \left( \EulerGamma@{\lambda} \right)^2 n!}$
  & $\dfrac{2^n \pochhammer{\lambda}{n}}{n!}$ & $0$
  & $\lambda > -\tfrac{1}{2}, \lambda \ne 0 $
----

Matched:
----
  \begin{minipage}[c]{1.0in}\centering Chebyshev\\ of first kind\end{minipage}
  & $\ChebyT{n}@{x}$
    \MarkDefn[T z 1 + chebyshev]{$\ChebyT{n}@{x}$}{Chebyshev polynomial of the first kind}%
  & $(-1,1)$ & $(1 - x^2)^{-\frac{1}{2}}$
  & $\begin{cases} \tfrac{1}{2}\pi, &\text{$n>0$} \\ \pi, &\text{$n = 0$} \end{cases}$
  & $\begin{cases} 2^{n-1}, & \text{$n > 0$} \\ 1, & \text{$n = 0$}\end{cases}$
  & $0$
  &
----

Matched:
----
  \begin{minipage}[c]{1.0in}\centering Chebyshev\\of second kind\end{minipage}
  & $\ChebyU{n}@{x}$
    \MarkDefn[U z 1 + chebyshev]{$\ChebyU{n}@{x}$}{Chebyshev polynomial of the second kind}%
  & $(-1,1)$ & $(1 - x^2)^{\frac{1}{2}}$
  & $\tfrac{1}{2} \pi$
  & $2^n$
  & $0$
  &
----

Matched:
----
  \begin{minipage}[c]{1.0in}\centering Chebyshev\\of third kind\end{minipage}
  & $\ChebyV{n}@{x}$
    \MarkDefn[V z 1 + chebyshev]{$\ChebyV{n}@{x}$}{Chebyshev polynomial of the third kind}%
  & $(-1,1)$ & $(1 - x)^{\frac{1}{2}} (1 + x)^{-\frac{1}{2}}$
  & $\pi$ & $2^n$
  & $\tfrac{1}{2}$
  &
----

Matched:
----
  \begin{minipage}[c]{1.0in}\centering Chebyshev\\of fourth kind\end{minipage}
i\[-16pt]
  & $\ChebyW{n}@{x}$
    \MarkDefn[W z 1 + chebyshev]{$\ChebyW{n}@{x}$}{Chebyshev polynomial of the fourth kind}%
  & $(-1,1)$ & $(1 - x)^{-\frac{1}{2}} (1 + x)^{\frac{1}{2}}$
  & $\pi$
  & $-\tfrac{1}{2}$
----

Matched:
----
  \begin{minipage}[c]{1.2in}\centering Shifted Chebyshev\\of first kind\end{minipage}
  & $\ChebyTs{n}@{x}$
    \MarkDefn[T z 3 + chebyshev]{$\ChebyTs{n}@{x}$}{shifted Chebyshev polynomial of the first kind}%
  & $(0,1)$
  & $(x - x^2)^{-\frac{1}{2}}$
  & $\begin{cases} \tfrac{1}{2} \pi, &\text{$n > 0$}
    \\ \pi, &\text{$n = 0$} \end{cases}$
  & $\begin{cases} 2^{2n-1}, &\text{$n > 0$} \\ 1, &\text{$n = 0$} \end{cases}$
  & $-\tfrac{1}{2} n$
  &
----

Matched:
----
  \begin{minipage}[c]{1.2in}\centering Shifted Chebyshev\\of second kind\end{minipage}
  & $\ChebyUs{n}@{x}$
    \MarkDefn[U z 3 + chebyshev]{$\ChebyUs{n}@{x}$}{shifted Chebyshev polynomial of the second kind}%
  & $(0,1)$ & $(x - x^2)^{\frac{1}{2}}$
  & $\tfrac{1}{8} \pi$
  & $2^{2n}$
  & $-\tfrac{1}{2}n$
  &
----

Matched:
----
  Legendre
  & $\LegendrePoly{n}@{x}$
    \MarkDefn[P z 1 + legendre]{$\LegendrePoly{n}@{x}$}{Legendre polynomial}%
  & $(-1,1)$ & $1$
  & $\ifrac{2}{(2n+1)}$
  & $\ifrac{2^n \pochhammer{\frac{1}{2}}{n}}{n!}$
  & $0$
  &
----

Matched:
----
  Laguerre
  & $\LaguerreL[\alpha]{n}@{x}$
   \MarkDefn[L z 3 + laguerre]{$\LaguerreL[\alpha]{n}@{x}$}{Laguerre (or generalized Laguerre) polynomial}%
   \MarkNotation[L z 1 + laguerre]{$\LaguerreL{n}@{x}$}{Laguerre polynomial}%
  & $(0,\infty)$
  & $e^{-x} x^{\alpha}$
  & $\ifrac{\EulerGamma@{n+\alpha+1}}{n!}$
  & $\ifrac{\opminus^n}{n!}$
  & $-n (n+\alpha)$
  & $\alpha > -1$
----

Matched:
----
  Hermite
  & $\HermiteH{n}@{x}$
   \MarkDefn[H z 1 + hermite]{$\HermiteH{n}@{x}$}{Hermite polynomial}%
  & $(-\infty,\infty)$
  & $e^{-x^2}$
  & $\pi^{\frac{1}{2}} 2^n n!$
  & $2^n$
  & $0$
  &
----

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM