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正则表达式以多行字符串形式匹配多个标题及其段落_Python_Regex_Multiline - Fatal编程技术网

Python正则表达式以多行字符串形式匹配多个标题及其段落

Python正则表达式以多行字符串形式匹配多个标题及其段落,python,regex,multiline,Python,Regex,Multiline,我试图在Python中创建一个正则表达式,它应该以多行字符串捕获标题和对应的文本。示例字符串: .Main Header This is the main paragraph in the text. Also this is another sentence. .Sub-Header This is secondary header and text. .Last Header And this is the last header in the text. 此处。主标题,。子标题和。最后一

我试图在Python中创建一个
正则表达式
,它应该以多行字符串捕获标题和对应的文本。示例字符串:

.Main Header
This is the main paragraph in the text. Also this is another sentence.
.Sub-Header
This is secondary header and text.
.Last Header
And this is the last header in the text.
此处
。主标题
。子标题
。最后一个标题
是段落的标题,接下来的几行(文本直到下一行)。标题“字符串”是文本的主体。因此,我的预期输出是:

Header1 - .Main Header, Text1 - This is the main paragraph in the text. Also this is another sentence.
Header2 - .Sub-Header, Text2 - This is secondary header and text.
Header3 - .Last Header, Text3 - And this is the last header in the text.
我曾试图组合一个
regex
,以满足这一期望,它几乎起作用,我面临的唯一挑战是捕获一个
点(.)
位于句子之间的文本(例如Text1),我的
regex
的停止标准是一个
换行符和
点(.)
由于下一个标题从一个点(.)
开始,因此我正在寻找帮助,以区分常规点和换行点作为我的停止标准

我现在的正则表达式是:

^(.\w+[^\n]+)\n([^\.]+)
对于
Text1

This is the main paragraph in the text
但应抓住:

This is the main paragraph in the text. Also this is another sentence.

也许试试下面的正则表达式

^(.\w+[^\n]+)\n(.*?)\.$
…它将从第二个匹配行中获取所有字符,直到该行末尾的最后一个点


演示:

事实上,没有必要使用
regex
@jizhaosama-我知道,但我想用regex来做,我几乎做到了。也许他们认为你让它更复杂了?我不知道。也许
^(.\w+[^\n]+)\n(.*?).$
从下一行到最后一行的最后一个点都有。演示:@MDR-谢谢你的帮助,我错过了这个把戏。你应该加上这个作为答案,然后我会把这个问题标记为完整的。