python - 使用 python re 库解析 Tex

标签 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}.*\\section{b}',a)
print(b)

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

最佳答案

您需要使用 re.DOTALL 标志来制作 .匹配换行符,如下所示:

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

关于python - 使用 python re 库解析 Tex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22384560/

相关文章:

Python 3 if-else 简写命令语法错误

python - 无法弄清楚为什么我收到 UnboundLocalError

python - 在 python 中使用 csv.reader 搜索删除

python - 如何将两个 keras 模型连接成一个模型?

c# - 是否存在永远不会匹配任何字符串的正则表达式?

javascript - 寻找正则表达式以匹配 ng-pattern 中的特定十进制格式

python - WSL——未找到命令

python - 单独线程中 Python 中的按键回显不显示第一个击键

python - 计算稀疏矩阵的相似度

javascript - 正则表达式在 JavaScript 中没有按预期工作