From 50c26dd8c80df2703e560bb52428c519465cef25 Mon Sep 17 00:00:00 2001 From: dhinendran Date: Tue, 15 Oct 2019 12:16:38 +0530 Subject: [PATCH] added code to remove multi comments --- prepGFG.py | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/prepGFG.py b/prepGFG.py index fc17109..7f9d7f8 100644 --- a/prepGFG.py +++ b/prepGFG.py @@ -1,17 +1,13 @@ import re -import pyperclip - -OUT = open('OUT.txt','r+') - -with open("IN.txt", "r") as IN: - data = IN.readlines() - for line in data: - x = re.search("^(\s)*/+.*",line) - if x == None : - print(line.rstrip()) - OUT.write(line.rstrip()+"\n") - -OUT.seek(0,0) -pyperclip.copy(OUT.read()) - -OUT.close() +input_file = 'IN.txt' +with open(input_file,"r") as f: + data = f.read() +# Remove multi empty lines +data=re.sub(r'\n\s*\n','\n',data,re.MULTILINE) +# Remove single line comments +data=re.sub(r'\n#.*', "", data) +# Remove multi comments in code +for x in re.findall(r'("[^\n]*"(?!\\))|(//[^\n]*$|/(?!\\)\*[\s\S]*?\*(?!\\)/)',data,8):data=data.replace(x[1],'') + +with open("OUT.txt", "w") as f: + f.write(data)