# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"""
\+\/\-[\s\S]+?//[\s\S]+?(^\};\s+$)
"""
test_str = ("block \"block1\"\n"
"{\n"
"ADDSFDJF\n"
"SDFSDFSDF\n"
"SDFSDFSDF\n\n"
" // subblock: subblock1 [master]\n"
" include \"/path/tofile/subblock1.conf\";\n"
"+/- subblock subblock1\n"
"// subblock: subblock1\n"
"subblock \"subblock1\"\n"
"{\n"
" type TYPE;\n"
" file \"name.file\";\n"
" details blah blah.\n"
" other {sdhsdf};\n"
" };\n"
"};\n\n"
" file \"dddd.file\";\n"
" details blah blah.\n\n"
" // subblock: subblock2\n"
" include \"/path/tofile/subblock2.conf\";\n"
"+/- subblock subblock2\n"
"// subblock: subblock2\n"
"subblock \"subblock2\"\n"
"{\n"
" type TYPE;\n"
" file \"name.file\";\n"
" details blah blah.\n"
" other {sdhsdf};\n"
" more fields\n"
"};\n"
"};")
subst = ""
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE | re.VERBOSE)
if result:
print (result)
# 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