简体   繁体   中英

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.

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 printer s):

^[ \t]*print\(

I you want to match only the print (and not all arguments), you can use :

^\s*(print)

See this live sample : http://refiddle.com/refiddles/57b56c8075622d22e8080000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM