import re
regex = re.compile(r"@media[^\{]+\{([^\{\}]*\{[^\}\{]*\})+[^\}]+\}")
test_str = (".visible-desktop {\n"
" display: inherit !important;\n"
"}\n\n"
"#intro .container {\n"
" margin:0 auto;\n"
" display:table;\n"
"}\n\n"
"@media (max-width: 480px) {\n"
" #intro .download-php { margin: 0; }\n"
"}\n\n"
"@media (min-width: 768px) and (max-width: 979px) {\n"
" .hidden-desktop {\n"
" display: inherit !important;\n"
" }\n"
" .visible-desktop {\n"
" display: none !important ;\n"
" }\n"
" .visible-tablet {\n"
" display: inherit !important;\n"
" }\n"
" .hidden-tablet {\n"
" display: none !important;\n"
" }\n\n"
"}\n\n"
"body, input, textarea {\n"
" font-family: \"Fira Sans\", \"Source Sans Pro\", Helvetica, Arial, sans-serif;\n"
" font-weight: 400;\n"
"}\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