import re def remove_comments(text): def replacer(match): s = match.group(0) if s.startswith('/'): #return " " # note: a space and not an empty string return "" # NOTE: an empty string instead of a space # since we dont care about wandom c expressions # like int/**/x=5 else: return s pattern = re.compile( r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"', re.DOTALL | re.MULTILINE ) return re.sub(pattern, replacer, text)