I am basically working to remove comments from a file(read) and write it to some file. The single line comments may be at the start of the line, or from in-between. The part from where the comment starts, to the next line, is to be removed.
Some answer suggested the below-mentioned code but it doesn't work for single line comments which are present after some useful code. I have some knowledge of lex, so I tried modifying the code to fix my need but I am stuck. Please Help.
import re
def stripComments(code):
code = str(code)
return re.sub(r'(?m)^ *#.*\n?', '', code)
print(stripComments("""#foo bar
Why so Serious? #This comment doesn't get removed
bar foo
# buz"""))
Expected output:
Why so Serious?
bar foo
Actual output:
Why so Serious? #This comment doesn't get removed
bar foo
[newline]
[newline]
You can use regex101.com to debug your regex and see what it's actually matching.
(?m)
changes the matching rules so that ^
matches the beginning of a line, rather than the beginning of the entire string
^ *
is matching the start of a line, followed by any number of space characters. (So hopefully there aren't any tabs!)
In plain English, your regex is matching only Python comments that come at the beginning of the line or after any number of spaces.
Other answers have already provided regexes to do what you want, so I won't repeat it here.