import re
regex = re.compile(r"^ *(<[^\r\n<>]*>)|(\s*<[^\r\n<>]*>\s*)(?=[^\r\n<]*$)|<\/?color[^<>]*>", flags=re.MULTILINE)
test_str = ("<color=#3D8CBF>using</color> <color=#00FFFF> aaa <Dont remove this tag> System</color>.Collections;\n"
"<color=#3D8CBF>using</color> <color=#00FFFF>System</color>.Collections.Generic;\n"
"<color=#3D8CBF>using</color> <color=#00FFFF>UnityEngine</color>;\n\n"
"<color=#3D8CBF>public</color> <color=#3D8CBF>class</color> ChangePosition : <color=#00FF00>MonoBehaviour</color> {\n\n"
" <color=#0000FF>// Update <color=#3D8CBF>is</color> called once per frame\n"
"</color>\n"
" <color=#3D8CBF>void</color> Update()\n"
" {\n"
" <color=#00FFFF>UnityEngine</color>.Profiling.Profiler.BeginSample(\"Test 1\");\n"
" Test1();\n"
" <color=#00FFFF>UnityEngine</color>.Profiling.Profiler.EndSample();\n\n"
" <color=#00FFFF>UnityEngine</color>.Profiling.Profiler.BeginSample(\"Test 2\");\n"
" Test2();\n"
" <color=#00FFFF>UnityEngine</color>.Profiling.Profiler.EndSample();\n"
" }\n"
"}\n")
subst = "\\1\\2"
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