Created
December 12, 2017 17:39
-
-
Save ep4sh/1c734f123c048f779a0fdd2532d59a43 to your computer and use it in GitHub Desktop.
Delete comments from source code files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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