# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r'(?<=\s)((?:docstring|some_detailed_notes)\s*=\s*\(?\s*(?://.*\n\s*)*)"((?:[^"\\]|\\.)+)"(\s*\)?\s*;)'
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"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
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