import re
regex = re.compile(r"(?:(['\"])(?>\\\1|(?!\1)[\w\W])*\1|\/\*[\w\W]*?\*\/)(*SKIP)(*F)|\h*\/\/.*+")
test_str = ("\"Testing a comment here // comment\" // comment\n\n"
"// comment\n\n"
"\"A bit \\\" trickier yet \\\" // comment\" // comment\n\n"
"// comment\n\n"
"// \"This should match since the comment came first\"\n\n"
"'Single \\' quotes // comment' // comment\n\n"
"'''\n"
"Ignore\n"
"// comment\n"
"\\''''\n\n"
"// comment\n\n"
"/*\n"
"multiline\n"
"// comment\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