lyen lyen - 3 months ago 14
Python Question

Regular expression matching lines that are not commented out

Given the following code

print("aaa")
#print("bbb")
# print("ccc")

def doSomething():
print("doSomething")


How can I use regular expression in Atom text editor to find all the
print
functions that are not commented out? I mean I only want to match the
print
s in
print("aaa")
and
print("doSomething")
.

I've tried
[^#]print
, but this also matches the
print
in
# print("ccc")
, which is something that is not desired.

[^# ]print
doesn't match any line here.

The reason I want to do this is that I want to disable the log messages inside a legacy project written by others.

Answer

Since you confirm my first suggestion (^(?![ \t]*#)[ \t]*print) worked for you (I deleted that first comment), I believe you just want to find the print on single lines.

The \s matches any whitespace, incl. newline symbols. If you need to just match tabs or spaces, use a [ \t] character class.

Use

^[ \t]*print

or (a bit safer in order not to find any printers):

^[ \t]*print\(
Comments