import re
regex = re.compile(r"(datatype\s+: 4\b(?:(?!datatype\s+: \d)[\s\S])*?(maxlength\s+:\s0\b))")
test_str = ("field {\n"
" id : 536870914\n"
" name : Set Field (Submit Mode)\n"
" datatype : 4\n"
" fieldtype : 1\n"
" create-mode : 2\n"
" option : 2\n"
" timestamp : 1489159658\n"
" owner : John Smith\n"
" last-changed : John Smith\n"
" length-units : 0\n"
" maxlength : 0\n"
" clob-store-opt : 0\n"
" menu-style : 1\n"
" qbe-match-op : 1\n"
" fulltext-optns : 0\n"
" permission : 12\\1\n"
"}\n"
"field {\n"
" id : 536870915\n"
" name : Schema Name\n"
" datatype : 4\n"
" fieldtype : 1\n"
" create-mode : 2\n"
" option : 1\n"
" timestamp : 1165057260\n"
" owner : John Smith\n"
" last-changed : John Smith\n"
" length-units : 0\n"
" maxlength : 30\n"
" clob-store-opt : 0\n"
" menu-style : 1\n"
" qbe-match-op : 1\n"
" fulltext-optns : 0\n"
" permission : 12\\1\n"
"}\n"
"field {\n"
" id : 536870916\n"
" name : Type\n"
" datatype : 4\n"
" fieldtype : 1\n"
" create-mode : 2\n"
" option : 1\n"
" timestamp : 1165057260\n"
" owner : John Smith\n"
" last-changed : John Smith\n"
" length-units : 0\n"
" maxlength : 30\n"
" clob-store-opt : 0\n"
" menu-style : 2\n"
" qbe-match-op : 1\n"
" fulltext-optns : 0\n"
" permission : 12\\1\n"
"}\n"
"field {\n"
" id : 536870917\n"
" name : Set Field (Query Mode)\n"
" datatype : 4\n"
" fieldtype : 1\n"
" create-mode : 2\n"
" option : 2\n"
" timestamp : 1489159658\n"
" owner : John Smith\n"
" last-changed : John Smith\n"
" length-units : 0\n"
" maxlength : 0\n"
" clob-store-opt : 0\n"
" menu-style : 1\n"
" qbe-match-op : 1\n"
" fulltext-optns : 0\n"
" permission : 12\\1\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