Table of contents
Group strings according to Door opened and Door closed:
If the last group does not have Door closed, the last piece of data is discarded:
Group strings according to Door opened and Door closed:
log_text = """
[20231201-211102] Door opened! test_videos/save_results/
[20231201-211102] order_idx: 1/228, 23111004594831533_0_216.mp4
[20231201-211108] Door closed!
[20231201-211109] Door opened! test_videos/save_results/
[20231201-211109] order_idx: 2/228, order_path = 1338.mp4
[20231201-211115] Get: score 0.52009
[20231201-211116] Door closed!
[20231201-211116] Door opened! /test_videos/save_results/
[20231201-211116] order_idx: 3/228, order.video_paths = high_res_20231110-133918_SW0031423111005385940276_0_256.mp4
[20231201-211147] Get:score 0.988185
[20231201-211147] Put: core 0.988185
[20231201-211151] Get: re 0.525953
[20231201-211152] Door closed!
"""
pattern = re.compile(r'(?=.*Door opened!)(?=.*Door closed!).*\n')
segments = pattern.split(log_text)
# 移除空字符串
segments = [segment.strip() for segment in segments if segment.strip()]
If the last group does not have a door closed, it will also be divided into one group.
If the last group does not have Door closed, the last piece of data is discarded:
pattern = re.compile(r'Door opened!(.*?)Door closed!', re.DOTALL)
matches = pattern.findall(log_text)
# 打印匹配结果
for match in matches:
print(match.strip())
print("---------------------")