我正在尝试使用正则表达式删除\\ [ - 16pt]和\\ \\ thinhline之间的代码部分。
这是我创建的正则表达式,适用于该格式的所有内容,除了我需要匹配的内容:
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))
以下是我将要测试的内容:
我只是试图使用这种模式分离出内容,但在pythex上测试之后,我意识到我的模式不起作用。如何修复此模式以使上述内容正确匹配?
答案 0 :(得分:1)
如下所示更改正则表达式
r'(?s)(\\\[-16pt]\n)(.*?)(\n *\\\\\n\\thinhline)'
要匹配文字\t
,您的模式必须为\\t
。从组索引2中获取字符串。
OR
使用外观而不是捕获组。
r'(?s)(?<=\\\[-16pt]\n).*?(?=\n *\\\\\n\\thinhline)'
从索引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$
&
----