import re
regex = re.compile(r"^config\s.*?(?=(?:\r?\n)+(?:config\s|\Z))", flags=re.MULTILINE | re.DOTALL)
test_str = ("config vdom\n"
"edit root\n"
"next\n"
"edit test\n"
"next\n"
"edit test2\n"
"next\n"
"end\n\n"
"config global\n"
"...\n"
"...\n"
"...\n"
"end\n"
"end\n\n"
"config vdom\n"
"edit root\n"
"config system\n"
"...\n"
"end\n"
"config ...\n"
"...\n"
"...\n"
"......\n"
"...\n"
"end\n"
"end\n\n"
"config vdom\n"
"edit test\n"
"config system\n"
"...\n"
"end\n"
"config ...\n"
"...\n"
"...\n"
"......\n"
"...\n"
"end\n"
"end\n\n"
"config vdom\n"
"edit test2\n"
"config system\n"
"...\n"
"end\n"
"config ...\n"
"...\n"
"...\n"
"...\n"
"end\n"
"end\n")
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