import re
regex = re.compile((r"(?sx) # Free-Spacing\n"
r"(?(DEFINE) # Define a few subroutines\n"
r" (?<double>“(?:(?!&[lr]squo;).)*”) # full set of doubles (no quotes inside)\n"
r" (?<single>‘(?:(?!&[lr]dquo;).)*’) # full set of singles (no quotes inside)\n"
r" (?<notquotes>(?:(?!&[lr][sd]quo;).)*) # chars that are not quotes\n"
r") # end DEFINE\n\n"
r"^ # Start of string\n"
r"(?: # Start non-capture group\n"
r" (?¬quotes) # Any non-quote chars\n"
r" &l(?<type>[sd])quo; # Opening quote, capture single or double type\n"
r" # any full singles, doubles, not quotes or recursion\n"
r" (?:(?&single)|(?&double)|(?¬quotes)|(?R))*\n"
r" &r\k<type>quo; # Closing quote of the correct type\n"
r")*+ # Repeat non-capture group\n"
r"(?¬quotes)\n"
r"\K\n"
r"&[lr][sd]quo; # unmatched quote\n"))
test_str = ("“Full Quote” The Left Quote is Missing”\n")
subst = ""
result = regex.sub(subst, test_str, count=1)
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