import re
regex = re.compile(r"^[^]*?`{3}(?:json)?\n(.*[^]*?)\n`{3}[^]*?$", flags=re.IGNORECASE)
test_str = ("```json\n"
"[\n"
" {\n"
" \"questionType\": \"singleselect\",\n"
" \"shortDescription\": \"Who will own the Intellectual Property rights developed during the provision of services?\",\n"
" \"fullDescription\": \"According to the agreement, who will own the Intellectual Property rights developed during the provision of services?\",\n"
" \"options\": [\n"
" \"The Consultant\",\n"
" \"The Client\",\n"
" \"Both parties\"\n"
" ]\n"
" },\n"
" {\n"
" \"questionType\": \"multipleselect\",\n"
" \"shortDescription\": \"What are included in Intellectual Property rights according to the agreement?\",\n"
" \"fullDescription\": \"What are included in Intellectual Property rights according to the agreement? (Select all that apply)\",\n"
" \"options\": [\n"
" \"Patents\",\n"
" \"Design\",\n"
" \"Trademark\",\n"
" \"Know-how\",\n"
" \"Trade secrets\",\n"
" \"Other\"\n"
" ]\n"
" },\n"
" {\n"
" \"questionType\": \"textinput\",\n"
" \"shortDescription\": \"What rights does the Client have regarding the projects and products resulting from the services?\",\n"
" \"fullDescription\": \"What specific rights does the Client have regarding the projects and products resulting from the services?\",\n"
" \"options\": []\n"
" }\n"
"]\n"
"```")
match = regex.search(test_str)
if match:
print(f"Match 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