您可以使用非贪婪修饰符修改regex组以减少捕获组数+(regex101):
import re
text = """\
===================
DateTimeStamp - ShortSummaryOfEntry
===================
1 Line 1 of text
2 Line 2 of text
3 Line last of text
===================
DateTimeStamp - ShortSummaryOfEntry
===================
4 Line 1 of text
5 Line 2 of text
6 Line last of text
===================
DateTimeStamp - ShortSummaryOfEntry
===================
7 Line 1 of text
8 Line 2 of text
9 Line last of text"""
pat = r"={19}\n(.+?)\n={19}\s*(.+?)\s*(?=={19}|\Z)"
for title, body in re.findall(pat, text, flags=re.S):
print(title)
print(body)
print("-" * 80)
打印:
DateTimeStamp - ShortSummaryOfEntry
1 Line 1 of text
2 Line 2 of text
3 Line last of text
--------------------------------------------------------------------------------
DateTimeStamp - ShortSummaryOfEntry
4 Line 1 of text
5 Line 2 of text
6 Line last of text
--------------------------------------------------------------------------------
DateTimeStamp - ShortSummaryOfEntry
7 Line 1 of text
8 Line 2 of text
9 Line last of text
--------------------------------------------------------------------------------