import re
regex = re.compile(r"(?m)(?<=^ *) {4}", flags=re.IGNORECASE)
test_str = ("{\n"
" \"Foo\": \"bar\",\n"
" \"Kestrel\": {\n"
" \"Endpoints\": {\n"
" \"Http\": {\n"
" \"Url\": \"http://*:2201\"\n"
" },\n"
" \"HttpsInlineCertStore\": {\n"
" \"Url\": \"https://*:2203\",\n"
" \"SslProtocols\": [\n"
" \"Tls12\",\n"
" \"Tls13\"\n"
" ],\n"
" \"Certificate\": {\n"
" \"Subject\": \"SubjectName\",\n"
" \"Store\": \"My\",\n"
" \"Location\": \"CurrentUser\",\n"
" \"AllowInvalid\": \"True\"\n"
" }\n"
" }\n"
" }\n"
" }\n"
"}")
subst = " "
result = regex.sub(subst, test_str)
if result:
print(result)
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