Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python正则表达式-TypeError_Python_Regex_Typeerror - Fatal编程技术网

Python正则表达式-TypeError

Python正则表达式-TypeError,python,regex,typeerror,Python,Regex,Typeerror,我编写了以下函数: def split(content): pattern = re.compile(r"""(\\\[-16pt]\n)(.*?)(\n\\\nthinhline)""", re.X | re.DOTALL) print(pattern) for m in pattern.finditer(content): print ("in for loop") print("Matched:\n----\n%s\n----\n" % m.g

我编写了以下函数:

def split(content):
   pattern = re.compile(r"""(\\\[-16pt]\n)(.*?)(\n\\\nthinhline)""", re.X | re.DOTALL)
   print(pattern)
   for m in pattern.finditer(content):
       print ("in for loop")
       print("Matched:\n----\n%s\n----\n" % m.group(2))
   print ("in split")
   return (0)
函数调用:

def replacement(content):
   split(content)
   pattern = re.compile(r'(?<=\\\\\[-16pt]\n)([\s\S]*?)(?=\\\\\n\\thinhline)')
   content= ' '.join(re.findall(pattern, content))
   print ("in replace")
   return content
编辑:


显然,这种模式不会匹配,为什么?我怎样才能创建一个模式来匹配\[-16pt]和\\thinhline之间的所有内容?

我在上测试了您的正则表达式,但它似乎失败了,原因有很多。首先,点
不适用于换行符,因此它在第一个换行符处停止。第二,我认为在最后一部分,你必须用很多斜杠,
\n\\\n\\\thinhline
。它正在转义
t
,因此它正在寻找制表符。这个正则表达式对我有效:
(\\[-16pt]\n)(.\s)*?(\\thinhline)

它表明对
split
的函数调用没有将字符串作为参数传递
pattern.finditer(str(content)):
最好也在问题中添加函数调用,这样其他人就可以更好地了解错误的位置内容是字符串。解决这个问题不需要知道字符串是什么。我认为有比这个更好的文件处理技术。对它们做更多的研究,然后试着调试它出错的原因。如果你发现了答案,请贴出来并告诉我们。到目前为止,坐下来远程预测可能出现的错误是不可能的。最好尝试单独解析每一行。一根线断开类型,可能会产生严重后果。试着调试一下。现在(因为已经是深夜了)再见,祝你一切顺利。
TypeError: expected string or buffer
import re

content = """ iaisjifgrhjoigehtoi w \\
\thinhline
\\[-16pt]
  Ultraspherical
\\
\thinhline
\\[-16pt]
  & $0$
  &
\\
\thinhline
\\[-16pt]
  & $\tfrac{1}{2} \pi$
  & $2^n$
  & $0$
  &
\\
\thinhline
\\[-16pt]
  & $\tfrac{1}{2}$
  &
\\
\thinhline
\\[-16pt]
  & $(-1,1)$ & $(1 - x)^{-\frac{1}{2}} (1 + x)^{\frac{1}{2}}$
  & $\pi$
  & $-\tfrac{1}{2}$
\\
\thinhline
\\[-16pt]
  & $(0,1)$
  & $(x - x^2)^{-\frac{1}{2}}$
  & $\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}
  &
\\
\thinhline
\\[-16pt]
  Legendre
\\
\thinhline
\\[-16pt]
  Shifted Legendre
\\
\thinhline
\\[-16pt]
  Laguerre
\\
\thinhline
\\[-16pt]
  Hermite
\\
\thinhline
\\[-16pt]
  Hermite
\\
\thinhline
\end{tabular}
\end{table}
\end{landscape}
%
\end{onecolumn*}
"""
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 pattern.finditer(content):
    print("Matched:\n----\n%s\n----\n" % m.group(1))