import re
regex = re.compile(r"""
'[^']*(?!\\)'(*SKIP)(*F) # Make sure we're not matching inside of quotes
|(?m-s:\s*(?:\-{2}|\#)[^\n]*$) # Single line comment
|(?:
\/\*.*?\*\/ # Multi-line comment
(?(?=(?m-s:[\t ]+$)) # Get trailing whitespace if any exists and only if it's the rest of the line
[\t ]+
)
)
""", flags=re.VERBOSE | re.DOTALL)
test_str = ("CALL agr_agreement(\n"
" 1,\n"
" 13075,\n"
" 'HBO Latin America--this is a test',\n"
" '(123) 456-7890',\n"
" 'M',\n"
" 'John Doe',\n"
" 'john@doecom',\n"
" 'Miami',\n"
" '(123) 456-7890',\n"
" 'Net 30',\n"
" 'Quarterly',\n"
" 0,\n"
" '2014-09-01',\n"
" '2014-09-01', /*\n"
" This is a multi''-line comment\n"
"*/ \n"
" '2017-08-31', -- single line comment\n"
" 36,\n"
" /*Inline, but multi-line comment*/'2015-03-26 06:59:44',\n"
" @status\n"
" );")
subst = ""
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