19 lines
571 B
Python
19 lines
571 B
Python
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)
|
|
|