使用先行查找将所有内容匹配 到 下一部分标题或字符串的末尾:
re_sections=re.compile(r"(?P<section>Sectiond)s*(?P<section_data>.+?)(?=(?:Sectiond|$))", re.DOTALL)
请注意,这也需要一个非贪婪的
.+?方法,否则它仍然会一直匹配到最后。
演示:
>>> re_sections=re.compile(r"(?P<section>Sectiond)s*(?P<section_data>.+?)(?=(?:Sectiond|$))", re.DOTALL)>>> for m in re_sections.finditer(text): print m.groupdict()... {'section': 'Section1', 'section_data': 'stuff belonging to section1nstuff belonging to section1nstuff belonging to section1n'}{'section': 'Section2', 'section_data': 'stuff belonging to section2nstuff belonging to section2nstuff belonging to section2'}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)