Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Using Regular Expressions to find comments in .btm files

Aug
2,828
147
I have been using the following to find comments in .btm files;

Code:
R:\>ffind /kvm /e"^( {0}::| {0}REM )") r:\zoom150.btm
::    File: zoom150.btm
:: Created: 2024-07-19
::   _4ver: 32.10
:: _winver: 10.0
::echo %_startpid
::activate "New tab*"
:: Changed to 13 (was 15) on 2024-07-21
:: Changed to 16 (was 13) on 2024-10-20

However, this neglects comments that do not appear at the beginning of a line.

No biggie, it still gives me the gist of the comments.

A recent conversation has encouraged me to try and find comments that not only appear at the beginning of a line,
but that begin in iff structures and such.

As Regular Expressions and I are not friends,
even after reviewing Beginning Regular Expressions
I gave in and asked DuckAI for assistance.

Link to DuckAI conversation: Shared Duck.ai Chat
(the link expires 30 days from now).

Thus, I have updated my Comments.btm script;
Code:
@SETLOCAL
@ECHO OFF
if isdir r:\temp set temp=r:\temp

iff %# eq 0 then
  echo USAGE: %_batchname theFile.btm
  quit
endiff

set theFile=%1

iff exist %theFile then
  ffind /kvm /e"^[ \t]*(::|REM )" %theFile
  ffind /kvm /e"^[ \t]*(::|REM )" %theFile > tmp9:
  echo ^nNumber of comment lines: %@inc[%@lines[tmp9:]]
else
  echo %theFile does not exist.
endiff

:eoj
ENDLOCAL

Output;
Code:
R:\>comments r:\zoom150.btm
::    File: zoom150.btm
:: Created: 2024-07-19
::   _4ver: 32.10
:: _winver: 10.0
                :: NEXT SENTENCE
::echo %_startpid
::activate "New tab*"
:: Changed to 13 (was 15) on 2024-07-21
:: Changed to 16 (was 13) on 2024-10-20

Number of comment lines: 9

I re-visted this due to the new LINES command in the FileUtils plugin,
which shows the number of comment lines in a .btm file.

FileUtils plugin: FileUtils Plugin

Joe
 
Last edited:
If you change {0} to {0,}, the expression will allow any number of spaces before the comment designator. Or you could replace {0} with * (which means the same thing).
 
Thanks @Jay Sage ,
but neither of those caught the line with the leading spaces.

Code:
R:\>ffind /kvm /e"^( {0,}::| {0,}REM )") r:\zoom150.btm
::    File: zoom150.btm
:: Created: 2024-07-19
::   _4ver: 32.10
:: _winver: 10.0
::echo %_startpid
::activate "New tab*"
:: Changed to 13 (was 15) on 2024-07-21
:: Changed to 16 (was 13) on 2024-10-20

R:\>ffind /kvm /e"^( *::| *REM )") r:\zoom150.btm
::    File: zoom150.btm
:: Created: 2024-07-19
::   _4ver: 32.10
:: _winver: 10.0
::echo %_startpid
::activate "New tab*"
:: Changed to 13 (was 15) on 2024-07-21
:: Changed to 16 (was 13) on 2024-10-20

R:\>comments.btm r:\zoom150.btm
::    File: zoom150.btm
:: Created: 2024-07-19
::   _4ver: 32.10
:: _winver: 10.0
                :: NEXT SENTENCE
::echo %_startpid
::activate "New tab*"
:: Changed to 13 (was 15) on 2024-07-21
:: Changed to 16 (was 13) on 2024-10-20

Number of comment lines: 9

Joe
 
Hey @Jay Sage it would seem the line;

Code:
                :: NEXT SENTENCE

...is preceeded with two tabs,
not spaces,
which explains why your solution did not work.

I originally missed the fact that the comment line was using tabs,
not spaces.

Joe
 
Yes @Jay Sage I changed the tabs to spaces,
and your solution works for that comment line.

Thanks for helping me remember that I used tabs instead of spaces.

Joe
 
Perhaps the regex engine has some expression to match whitespace characters in general, instead of just tabs?
 
You can use this:

ffind /kvm /e"^\s*(::|REM)"​

That expression is looking for the beginning of a line (^) followed by any amount of white space (\s*) followed by either "::" or "REM".

An easy way to learn about and experiment with regular expressions is to open the Regular Expression Analyzer" tool that is part of Take Command. The help file also includes a lot of detailed information.
 
I just discovered that the expression above is case-sensitive, which is not what one wants (since "rem" or "ReM") can begin a comment). One needs to add the option (?i).

ffind /kvm /e"^\s*(::|(?i)REM)"

The option could be inserted at the beginning of the entire expression to make the case independence apply everywhere in the test.

ffind /kvm /e"(?i)^\s*(::|REM)"

In addition, one should require that "REM" be followed by white space.

ffind /kvm /e"(?i)^\s*(::|REM\s)"
 
I occasionally do the likes of this.

Code:
echo foo & REM comment

This works too (recently tested).

Code:
echo foo & :: comment

So how would you find them? You'd need some text, then an unescaped & (whitespace on either side), then REM or ::

I don't feel like trying that one. ^ is just too special.

I thought of suggesting you TEE TMP9: but that apparently doesn't work (sigh!).

How do I find a discussion of TMP0: - TMP9: in the help?
 
You can use this:

ffind /kvm /e"^\s*(::|REM)"​
That produces a blank line in the search results;

Code:
R:\>ffind /kvm /e"^\s*(::|REM)" zoom150.btm
::    File: zoom150.btm
:: Created: 2024-07-19
::   _4ver: 32.10
:: _winver: 10.0
                :: NEXT SENTENCE
::echo %_startpid
::activate "New tab*"

:: Changed to 13 (was 15) on 2024-07-21
:: Changed to 16 (was 13) on 2024-10-20

Refer to Post #7 in this thread for why that happens.
Reference: Using Regular Expressions to find comments in .btm files

Joe
 
OK, I'll look into your comment. Meanwhile, I was worrying about lines beginning with the string REM followed by another character, which makes it not a comment. Does this work? It seems to for me.

ffind /kvm /e"(?i)^\s*(::|rem(\s|$))"
 
I still get the blank line.

Code:
R:\>ffind /kvm /e"(?i)^\s*(::|rem(\s|$))" r:\zoom150.btm
::    File: zoom150.btm
:: Created: 2024-07-19
::   _4ver: 32.10
:: _winver: 10.0
                :: NEXT SENTENCE
::echo %_startpid
::activate "New tab*"

:: Changed to 13 (was 15) on 2024-07-21
:: Changed to 16 (was 13) on 2024-10-20

From DuckAI;
\s includes newline characters in TCC’s default regular-expression syntax.
So \s* can consume past the end of a line—and across blank lines—before matching :: or REM.
That explains why the output includes an empty line.
The documented whitespace class includes \n and \r.

This works for me;
Code:
ffind /kvm /e"^[ \t]*(::|REM )"

Joe
 
Back
Top