import re
regex = re.compile(r'(?<=\s)((?:docstring|some_detailed_notes)\s*=\s*\(?\s*(?://.*\n\s*)*)"((?:[^"\\]|\\.)+)"(\s*\)?\s*;)', flags=re.MULTILINE)
test_str = ("class myClassA(myBaseClass) {\n\n"
" docstring = // End-of-line-comments possible\n"
"\"\n"
"This is my class description docstring stored in a string variable inherited from\n"
"myBaseClass.\n"
"The content of this string, INCLUDING INDENTATION, MUST NOT be changed.\n"
"\";\n\n"
"docstring = (\n"
"\"\n"
"Enclosed in parentheses\n"
"\"\n"
");\n\n"
" // This variable is declared as string has a mismatched indentation:\n"
"string some_detailed_notes = \"Some string like this is also possible.\n"
" Also with\n"
" some really\n"
" strange indentation\n"
" and `inline code`, ```code blocks``` $\\\\text{LaTeX}$ $$\\\\frac{1}{2}$$\n"
":::{hint}\n"
"some admonitions\n"
":::\n"
"and all kind of special characters such as:\n"
"\\\"\n"
"(...)\n"
"{...}\n"
"[...]\n"
";.,\n"
"etc.\n\n"
"which MUST be preserved.\";\n\n"
" string some_other_string = \"\n"
" do NOT touch this, only docstring and some_detailed_notes\n"
" \";\n"
"}")
subst = "\\1R\"\"\"\"\\2\"\"\"\"\\3"
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