Page 1 of 1

Regex: Find all non-comment, non empty lines

Posted: Tue May 19, 2020 1:35 pm
by NotNull
Context: Working on an AutoHotKey script in Notepad++ (meaning PCRE regex syntax)

In this script are quite a few (700) comment lines and empty lines (possibly including some spaces and tabs).
AHK comment lines start with a ";"

I can find them with:

Code: Select all

^[ \t]*;|^[ \t]*$
How can I find the remaining (350) lines of code?. I should know this, but at this moment ...

Re: Regex: Find all non-comment, non empty lines

Posted: Tue May 19, 2020 3:36 pm
by therube
Not sure what you're asking?

You want to find them all using NP++?

Looks like it would be, 'Find All in Current Document', but you know that. So... ?

Re: Regex: Find all non-comment, non empty lines

Posted: Tue May 19, 2020 3:43 pm
by ovg
Hi NotNull
try:

Code: Select all

^[ \t]*;|^[ \t]*\R

Re: Regex: Find all non-comment, non empty lines

Posted: Tue May 19, 2020 4:23 pm
by NotNull
Thanks! Unfortunately that didn't work. It still finds all the lines I don't want (but does that well).

A double negative lookahead (if that is the right terminology) does work (I'm a little less "groggy" now :)):

Code: Select all

^(?![ \t]*;)(?![ \t]*$)

Re: Regex: Find all non-comment, non empty lines

Posted: Tue May 19, 2020 4:30 pm
by therube
Maybe this will give you non-empty,
^.*\S+
.

Re: Regex: Find all non-comment, non empty lines

Posted: Tue May 19, 2020 5:16 pm
by NotNull
therube wrote:
Tue May 19, 2020 3:36 pm
Not sure what you're asking?
Sorry; I was rather "groggy" when posting the question.

Script:

Code: Select all

<space><tab>; comment<CRLF>
<tab><tab><CRLF>
code1<CRLF>
<tab>code2<CRLF>
; comment<CRLF>
<tab><tab><CRLF>
The search (indeed, using 'Find All in Current Document') should report only both code lines.

Solution: the lookahead approach. Search for:

Code: Select all

^(?![ \t]*;)(?![ \t]*$)

Re: Regex: Find all non-comment, non empty lines

Posted: Tue May 19, 2020 5:31 pm
by ovg
Strange, I tested with regex101.com and RegexBuddy - working fine... It seems this is NP++ fault

Re: Regex: Find all non-comment, non empty lines

Posted: Tue May 19, 2020 5:44 pm
by NotNull
ovg wrote:
Tue May 19, 2020 5:31 pm
Strange, I tested with regex101.com and RegexBuddy - working fine... It seems this is NP++ fault
Compare this:
2020-05-19 19_39_25-Online regex tester and debugger_ PHP, PCRE, Python, Golang and JavaScript.png
with this:
2020-05-19 19_37_57-Online regex tester and debugger_ PHP, PCRE, Python, Golang and JavaScript.png
The first one isreporting all the non-code lines, the second one all the code lines.
I bet this is all a misunderstanding due to my poor descriptiom :)
I wanted to see only the code-lines.

(added .* to both expressions for better visibility).

Re: Regex: Find all non-comment, non empty lines

Posted: Tue May 19, 2020 6:22 pm
by ovg
:D Ok :mrgreen:

Re: Regex: Find all non-comment, non empty lines

Posted: Sun Jun 07, 2020 11:55 am
by RegexNinja
If it helps readability, you can always throw ORs into the LookAhead with \s.
^(?!\s*;|\s*$) should give the same results.

Re: Regex: Find all non-comment, non empty lines

Posted: Sun Jun 07, 2020 3:07 pm
by NotNull
Nice!

I had mixed results in the past with using \s [1], so on auto-pilot I use an alternative. But in this case it fits well.

[1] Can't remember where and when. Maybe even a different regex flavour.