Regex: Find all non-comment, non empty lines

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

Regex: Find all non-comment, non empty lines

Post 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 ...
therube
Posts: 4580
Joined: Thu Sep 03, 2009 6:48 pm

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

Post 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... ?
ovg
Posts: 294
Joined: Thu Oct 27, 2016 7:19 pm

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

Post by ovg »

Hi NotNull
try:

Code: Select all

^[ \t]*;|^[ \t]*\R
NotNull
Posts: 5167
Joined: Wed May 24, 2017 9:22 pm

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

Post 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]*$)
therube
Posts: 4580
Joined: Thu Sep 03, 2009 6:48 pm

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

Post by therube »

Maybe this will give you non-empty,
^.*\S+
.
NotNull
Posts: 5167
Joined: Wed May 24, 2017 9:22 pm

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

Post 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]*$)
ovg
Posts: 294
Joined: Thu Oct 27, 2016 7:19 pm

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

Post by ovg »

Strange, I tested with regex101.com and RegexBuddy - working fine... It seems this is NP++ fault
NotNull
Posts: 5167
Joined: Wed May 24, 2017 9:22 pm

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

Post 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
2020-05-19 19_39_25-Online regex tester and debugger_ PHP, PCRE, Python, Golang and JavaScript.png (18.16 KiB) Viewed 22795 times
with this:
2020-05-19 19_37_57-Online regex tester and debugger_ PHP, PCRE, Python, Golang and JavaScript.png
2020-05-19 19_37_57-Online regex tester and debugger_ PHP, PCRE, Python, Golang and JavaScript.png (17.49 KiB) Viewed 22795 times
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).
ovg
Posts: 294
Joined: Thu Oct 27, 2016 7:19 pm

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

Post by ovg »

:D Ok :mrgreen:
RegexNinja
Posts: 18
Joined: Sat Apr 11, 2020 2:45 pm

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

Post by RegexNinja »

If it helps readability, you can always throw ORs into the LookAhead with \s.
^(?!\s*;|\s*$) should give the same results.
NotNull
Posts: 5167
Joined: Wed May 24, 2017 9:22 pm

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

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