更新时间: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)