re.findall多行python

re.findall多行python,python,regex,Python,Regex,带有re.M的re.findall找不到我要搜索的多行 我试图从文件中提取与模式匹配的所有多行字符串 文件book.txt中的示例: Title: Le Morte D'Arthur, Volume I (of II) King Arthur and of his Noble Knights of the Round Table Author: Thomas Malory Editor: William Caxton Release Date: March, 1998 [E

带有re.M的re.findall找不到我要搜索的多行

我试图从文件中提取与模式匹配的所有多行字符串

文件
book.txt
中的示例:

Title: Le Morte D'Arthur, Volume I (of II)
       King Arthur and of his Noble Knights of the Round Table

Author: Thomas Malory

Editor: William Caxton

Release Date: March, 1998  [Etext #1251]
Posting Date: November 6, 2009

Language: English

Title: Pride and Prejudice

Author: Jane Austen

Posting Date: August 26, 2008 [EBook #1342]
Release Date: June, 1998
Last Updated: October 17, 2016

Language: English
以下代码仅返回第一行
Le Morte D'Arthur,第一卷(共二卷)

我希望输出是

[《亚瑟王之死》(Le Morte D'Arthur,第一卷,第二卷)\n亚瑟王及其高贵的圆桌骑士》,《傲慢与偏见》]

澄清一下,
-第二行是可选的,它存在于某些文件中,而不存在于其他文件中。第二行后面还有更多我不想读的文字。
-使用
re.findall(r'Title:(.+\n.+)$),text,flags=re.MULTILINE)
可以工作,但如果第二行正好为空,则失败。
-我正在运行python3.7。
-我正在将txt文件转换为字符串,然后在str上运行
re

-以下内容也不起作用:
re.findall(r'^Title:\s(+)$,text,re.s)


re.findall(r'^Title:\s(+)$,text,re.DOTALL)

我猜可能是这个表达式

(?<=Title:\s)(.*?)\s*(?=Author)

您可以将正则表达式与
DOTALL
标志一起使用,以允许
匹配换行符:

re.findall('^Title:\s(.+)$', book, re.DOTALL)
输出:

Le Morte D'Arthur, Volume I (of II)\n       King Arthur and of his Noble Knights of the Round Table

默认情况下,点与换行符不匹配,请添加
re.S
Try:
re.findall(r'Title:(.+\n.+)$,text,flags=re.MULTILINE)
,如下面的一个答案所示,
re.DOTALL
将是实现您的目标的简单方法要回答您的问题,请添加以下详细信息:1)您是如何从文件读取数据的?添加此代码,2)其他示例是什么?你如何用语言描述这种模式?Hoew想知道什么时候匹配应该停止?这对我来说不起作用,不确定为什么,我正在运行python3.7这可以工作,但它最终会读取所有行,我只想让它读取
标题后的一行:
行我添加了足够的数据,应该足以用于演示,并更新我的OP以反映相同的内容。非常感谢,我不确定
作者
是否总是排在
标题
之后,所以我对它做了一些修改,效果不错,
["Le Morte D'Arthur, Volume I (of II)\n       King Arthur and of his Noble Knights of the Round Table\n\n", "Le Morte D'Arthur, Volume I (of II)\n       King Arthur and of his Noble Knights of the Round Table"]
re.findall('^Title:\s(.+)$', book, re.DOTALL)
Le Morte D'Arthur, Volume I (of II)\n       King Arthur and of his Noble Knights of the Round Table