at master 538 B view raw
1"""Removes C/C++ style comments from text. 2 3Gratefully adapted from https://stackoverflow.com/a/241506 4""" 5import re 6 7comment_pattern = re.compile(r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"', re.DOTALL | re.MULTILINE) 8 9 10def _comment_stripper(match): 11 """Removes C/C++ style comments from a regex match. 12 """ 13 s = match.group(0) 14 return ' ' if s.startswith('/') else s 15 16 17def comment_remover(text): 18 """Remove C/C++ style comments from text. 19 """ 20 return re.sub(comment_pattern, _comment_stripper, text)