import re
regex = re.compile(r'(^\s+(?P<att_name>[^\s]+)\s+(?P<att_value>[^\s{]+|"[^"]+?")\s?(?P<att_body>{[\s\S]+?})?)$', flags=re.MULTILINE)
test_str = ("\n"
" rel_tgt_id 2\n"
" IncomingUser \"user1234 secret123456\"\n"
" GROUP access_group{\n"
" LUN 0 dev2-0\n"
" LUN 1 dev2-1\n"
" LUN 2 dev2-2\n"
" LUN 3 dev2-3\n"
" LUN 4 dev2-4\n"
" LUN 5 dev2-5\n"
" LUN 6 dev2-6\n"
" LUN 7 dev2-7\n"
" LUN 8 dev2-8\n"
" LUN 9 dev2-9\n"
" INITIATOR iqn.1994-09.org.freebsd:*\n"
" }\n"
" enabled 1\n"
" MaxSessions 10\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