Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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
使用pythonre库解析Tex_Python_Regex_Python 3.x - Fatal编程技术网

使用pythonre库解析Tex

使用pythonre库解析Tex,python,regex,python-3.x,Python,Regex,Python 3.x,我想解析.tex文件的以下部分 \section{a} some random lines with lot of special characters \subsection{aa} somehthing here too \section{b} 我希望内容包含在\section{a}和\section{b}中,因此我在python中尝试了以下代码 import re a="my tex string mentioned above" b=re.findall(r'\\section{a}

我想解析.tex文件的以下部分

\section{a}
some random lines with lot 
of special characters
\subsection{aa}
somehthing here too
\section{b}
我希望内容包含在
\section{a}
\section{b}
中,因此我在python中尝试了以下代码

import re
a="my tex string mentioned above"
b=re.findall(r'\\section{a}.*\\section{b}',a)
print(b)

但是我得到了
b=[]
。我错在哪里?

您需要使用re.DOTALL标志来进行修改。匹配换行符,如下所示:

b=re.findall(r'\\section{a}.*\\section{b}',a,re.DOTALL)

作为元字符的花括号不应该用
\{
转义吗?@halex似乎很多解析器都将它们视为文本,如果它们里面没有数字的话。