Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find lines which are beginning from #, with exceptions

I've C++ file, like:

#include <stdio>
#if 0
int a=1;
#endif
.....

How to write regular expression to find all lines which are beginning from #, except the lines which are starting from the #include keyword?

like image 937
zuko Avatar asked Nov 26 '25 09:11

zuko


2 Answers

Try this regex:

/^#(?!include).*$/gm
like image 90
BoltClock Avatar answered Nov 28 '25 23:11

BoltClock


/^\s*#(?!include\b).*$/m

matches a line that starts with optional whitespace, then a #, unless followed by include.

So, in JavaScript:

result = subject.match(/^\s*#(?!include\b).*$/mg);
like image 36
Tim Pietzcker Avatar answered Nov 29 '25 00:11

Tim Pietzcker