Skip to content

Instantly share code, notes, and snippets.

@ep4sh
Created December 12, 2017 17:39
Show Gist options
  • Save ep4sh/1c734f123c048f779a0fdd2532d59a43 to your computer and use it in GitHub Desktop.
Save ep4sh/1c734f123c048f779a0fdd2532d59a43 to your computer and use it in GitHub Desktop.
Delete comments from source code files
#developed by @ep4sh
import re
import sys
#################
# USAGE (from terminal):
# python sourceCodeRemover.py <source_code_name>
###############
# .NET +
# Python +-
# JS +
# JAVA +
# GO +
# C / C++ +
# CSS +
# PHP +
# Ruby +-
# Shell +
def rmCComment(string):
string = re.sub(re.compile("/\*.*?\*/",re.DOTALL) ,"" ,string)
string = re.sub(re.compile(r"(\".*?\"|\'.*?\')|(/\*.*?\*/|//[^\r\n]*$)") ,"" ,string)
return string
def rmShellComment(string):
string = re.sub(re.compile(r"(#.*)",re.MULTILINE),"" ,string)
return string
with open(sys.argv[1],"r") as file:
for line in file:
with open(sys.argv[1] + "_new", "a") as file2:
file2.writelines(rmShellComment(rmCComment(line)))
file2.close()
file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment