import re
regex = re.compile(r"(Demand.*?\d{2}[\n\r])")
test_str = ("++ PLANNING ITERATIONS of DEMAND 337 ++\n"
"=========================================\n\n"
" Demand: 337 Event: 1189.001 Object/State: 7058/0 Tier: 0 Start: 1608130700 Duration: 90 At: 19-7-2016 16:19:36\n"
" Demand: 337 Event: 1190.001 Object/State: 7059/0 Tier: 0 Start: 1608130830 Duration: 330 At: 19-7-2016 16:19:36\n"
" Demand: 337 Event: 1191.001 Object/State: 7060/0 Tier: 0 Start: 1608140000 Duration: 360 At: 19-7-2016 16:19:36\n\n"
"++ EVENT PLAN of DEMAND 337 ++\n"
"===============================\n\n"
"event_time(1242.001,1,1609070800,1609071430)\n"
"event_time(1241.001,1,1609060800,1609061430)\n"
"event_time(1240.001,1,1609050800,1609051430)\n\n\n"
"++ PLANNING ITERATIONS of DEMAND 174 ++\n"
"=========================================\n\n"
" Demand: 174 Event: 212.001 Object/State: 6948/0 Tier: 0 Start: 1609010800 Duration: 390 At: 19-7-2016 16:19:38\n"
" Demand: 174 Event: 213.001 Object/State: 6949/0 Tier: 0 Start: 1609020800 Duration: 390 At: 19-7-2016 16:19:38\n\n"
"++ EVENT PLAN of DEMAND 174 ++\n"
"===============================\n\n"
"event_time(213.001,1,1609020800,1609021430)\n"
"event_time(212.001,1,1609010800,1609011430)")
matches = regex.finditer(test_str)
for match_num, match in enumerate(matches, start=1):
print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}")
for group_num, group in enumerate(match.groups(), start=1):
print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html