且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

提取两个定义文本之间的文本信息

更新时间:2022-12-11 08:41:15

使用(?s),这将多行视为单行.因此,点匹配所有字符,包括换行符.

Use (?s), this is consider multiple line as a single line. So dot match all characters including newlines.

pattern = re.compile(r'(?s)begin MEASUREMENT[\s](.*?)end MEASUREMENT')

所以尝试一下,

import re
path = "py.txt"
file = open(path, 'r')
lines = file.read()
pattern = re.compile(r'(?s)begin MEASUREMENT[\s](.*?)end MEASUREMENT')
result = re.findall(pattern, lines)
print result[0]

已编辑

t = "XYZ"
pattern = re.compile(r'(?s)begin MEASUREMENT\s+((%s).*?)end MEASUREMENT'%t)