# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^(?!.*@media)[\t ]*([a-zA-Z#.:*[][^{\/]*\s*){[\s\S]*?}"
test_str = ("a {color: red !important;}\n"
" \n"
"#p1 {\n"
" margin-top: 20px !important\n"
" }\n\n"
"/* Client-specific styles */\n\n"
" .ExternalClass, \n"
" .ExternalClass p, \n"
" .ExternalClass span, \n"
" .ExternalClass font, \n"
" .ExternalClass td, \n"
" .ExternalClass div {\n"
" line-height: 100%;\n"
" }\n"
" \n"
" a {}\n\n"
"@media query { \n"
"a { \n"
"display:none \n"
"} \n"
"}\n\n\n"
" a\n"
" { display:none } \n"
"}\n\n"
"/* Media query for mobile viewport\n"
" #asset_container * Developer: hero graphics should be 2 x width for HD rendering.\n"
" */ \n"
" @media only screen and (max-width: 480px){\n"
" table[class=max-width-pad] {\n"
" max-width: 100% !important;\n"
" width: 100% !important;\n"
" padding-top: 15px !important;\n"
" height: auto !important;\n"
" }\n"
" #asset_container table[class=max-width]{\n"
" max-width: 100% !important;\n"
" width: 100% !important;\n"
" height: auto !important;\n"
" }\n"
" #asset_container table[class=container]{\n"
" margin: 0 auto !important;\n"
" }\n"
" #asset_container .desktop-masthead{\n"
" display: none !important;\n"
" }\n"
" #asset_container *[class].hidden. *[class=desktop-masthead]{\n"
" display: none !important;\n"
" }\n"
" #asset_container *[class].elastic{\n"
" width: 100% !important;\n"
" height: auto !important;\n"
" }\n"
" #asset_container *[class].centered{\n"
" text-align: center !important;\n"
" }\n"
" #asset_container *[class].fluid, #asset_container [class=fluid-mob]{\n"
" width: 100% !important;\n"
" }\n"
" [class=fluid-mob] {\n"
" #asset_container position: relative;\n"
" }\n"
" .mobile-show{\n"
" display: table-cell !important;\n"
" width: auto !important;\n"
" height: auto !important;\n"
" max-height: none !important;\n"
" overflow: visible !important;\n"
" visibility: visible !important;\n"
" position: relative !important;\n"
" text-align: center !important;\n"
" }\n"
" #asset_container .show-mob{\n"
" display: block !important;\n"
" max-height: none !important;\n"
" width: auto !important;\n"
" visibility: visible !important;\n"
" }\n"
" #asset_container *[class].mob-masthead{\n"
" width: 100% !important;\n"
" display: block !important;\n"
" height: auto !important;\n"
" max-height: none !important;\n"
" padding: 0 !important;\n"
" }\n"
" #asset_container *[class].mob-masthead-img{\n"
" position: absolute !important;\n"
" top: 0px !important;\n"
" display: block !important;\n"
" height: auto !important;\n"
" max-height: none !important;\n"
" padding: 0 !important;\n"
" width: auto !important;\n"
" }\n"
" [class=fluid-mob] {\n"
" display: table-cell !important;\n"
" width: auto !important;\n"
" height: auto !important;\n"
" max-height: none !important;\n"
" overflow: visible !important;\n"
" visibility: visible !important;\n"
" position: relative !important;\n"
" text-align: center !important;\n"
" }\n"
" }\n")
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
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