if s.startswith('/'):
    return ' '
else:
    return s
pattern = re.compile(
    '//.*?$|/\\*.*?\\*/|\\\'(?:\\\\.|[^\\\\\\\'])*\\\'|"(?:\\\\.|[^\\\\"])*"',
    re.DOTALL | re.MULTILINE)
return re.sub(pattern, replacer, text)
Python snippet to remove C and C++ comments