You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

20 lines
538 B

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