阅读背景:

使用带有行数的Python Regex

来源:互联网 

This is my test code:

这是我的测试代码:

\thinhline
\[-16pt]
Jacobi
  & $\JacobiP{\alpha}{\beta}{n}@{x}$
  & $(-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]
Ultraspherical(Gegenbauer)
  & $\Ultraspherical{\lambda}{n}@{x}$
  & $(-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!}$ & 

This is my test code:

这是我的测试代码:

\thinhline
\\[-16pt]
Jacobi
  & $\JacobiP{\alpha}{\beta}{n}@{x}$
  & $(-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]
Ultraspherical(Gegenbauer)
  & $\Ultraspherical{\lambda}{n}@{x}$
  & $(-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 $
\\

I have created a pattern to identify the pattern of everything in between "\thinhline \\[-16pt]" and "\\".

我创建了一个模式来识别“\thinhline \[-16pt]”和“\”之间的所有模式。

How would I find the line count of the lines beginning with ampersands, "&", of the instance of the pattern found?

我怎么能找到从ampersands开始的行数,“&”,这个模式的实例的实例呢?

For example I would want this returned for the sample code:

例如,我希望这个示例代码返回:

Jacobi: 8
Ultraspherical(Gegenbauer): 6

3 个解决方案

#1


1  

Try this regex ^\s*& with the MULTILINE (re.M) flag:

试试这个regex ^ \ s * &多行(re.M)旗:

import re

text = """
  & $\Ultraspherical{\lambda}{n}@{x}$
  & $(-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 $

"""

print(len(re.findall('^\s*&', text, re.M)))

prints 6, which is the number of lines beginning with &

打印6,即以&开头的行数

#2


1  

How about:

如何:

import re

s = text.split('\\thinhline\n\\\\[-16pt]\n')[1:]
res = [re.split("\n\s*&", a) for a in s]

[a[0] + ": " + str(len(a)-1) for a in res]
#['Jacobi: 8', 'Ultraspherical(Gegenbauer): 6']

#3


1  

for i,line enumerate(my_text.splitlines(),1):
    if line.strip().startswith("&"):
       print line,"On line",i

might be what you want ... I dont think regex is the right answer for this

可能是你想要的……我认为regex不是正确的答案


$ & $\lambda > -\tfrac{1}{2}, \lambda \ne 0 $ \ \thinhline



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: