import re
regex = re.compile(r"^id(?:_p)?\h+\"(.*)\"(?:(?:\Rstr(?:\[\d+])?\h*)\"\")+", flags=re.MULTILINE)
test_str = ("id \"text 1\"\n"
"str \"\"\n\n"
"id \"text 2\"\n"
"str \"\"\n\n"
"id \"text 3\"\n"
"str \"\"\n\n"
"id \"text 4\"\n"
"str \"\"\n\n"
"id \"text 5\"\n"
"str \"\"\n\n"
"id \"text 6\"\n"
"id_p \"text 6-2\"\n"
"str[0] \"\"\n"
"str[1] \"\"")
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