Regex: Find all non-comment, non empty lines

Off-topic posts of interest to the "Everything" community.
Post Reply
NotNull
Posts: 2156
Joined: Wed May 24, 2017 9:22 pm

Regex: Find all non-comment, non empty lines

Post by NotNull » Tue May 19, 2020 1:35 pm

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 ...

therube
Posts: 2610
Joined: Thu Sep 03, 2009 6:48 pm

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

Post by therube » Tue May 19, 2020 3:36 pm

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... ?

ovg
Posts: 207
Joined: Thu Oct 27, 2016 7:19 pm

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

Post by ovg » Tue May 19, 2020 3:43 pm

Hi NotNull
try:

Code: Select all

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

NotNull
Posts: 2156
Joined: Wed May 24, 2017 9:22 pm

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

Post by NotNull » Tue May 19, 2020 4:23 pm

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]*$)

therube
Posts: 2610
Joined: Thu Sep 03, 2009 6:48 pm

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

Post by therube » Tue May 19, 2020 4:30 pm

Maybe this will give you non-empty,
^.*\S+
.

NotNull
Posts: 2156
Joined: Wed May 24, 2017 9:22 pm

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

Post by NotNull » Tue May 19, 2020 5:16 pm

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]*$)

ovg
Posts: 207
Joined: Thu Oct 27, 2016 7:19 pm

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

Post by ovg » Tue May 19, 2020 5:31 pm

Strange, I tested with regex101.com and RegexBuddy - working fine... It seems this is NP++ fault

NotNull
Posts: 2156
Joined: Wed May 24, 2017 9:22 pm

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

Post by NotNull » Tue May 19, 2020 5:44 pm

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).
You do not have the required permissions to view the files attached to this post.

ovg
Posts: 207
Joined: Thu Oct 27, 2016 7:19 pm

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

Post by ovg » Tue May 19, 2020 6:22 pm

:D Ok :mrgreen:

RegexNinja
Posts: 11
Joined: Sat Apr 11, 2020 2:45 pm
Location: OrionCygnusArm, MilkyWay

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

Post by RegexNinja » Sun Jun 07, 2020 11:55 am

If it helps readability, you can always throw ORs into the LookAhead with \s.
^(?!\s*;|\s*$) should give the same results.

NotNull
Posts: 2156
Joined: Wed May 24, 2017 9:22 pm

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

Post by NotNull » Sun Jun 07, 2020 3:07 pm

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.

Post Reply