Page 1 of 1

FINDSTR \s regexp meaning

Posted: 09 Jun 2021 12:48
by findstr
Hello!

Code: Select all

C:\>ECHO /? | FINDSTR /R "\s"
 ECHO [message]
How does FINDSTR interpret \s regexp?
It produces some strange results, try:
FOR /? | FINDSTR /R "\s"
DIR /? | FINDSTR /R "\s"
netstat -a | FINDSTR /R "\s"

DIR /? | FINDSTR /R "\t"
Produces weird output.
ECHO /? | FINDSTR /R "\t"
Produces no output.

How does it work?

Re: FINDSTR \s regexp meaning

Posted: 10 Jun 2021 03:11
by aGerman
Well, you provided some commands. But you didn't tell what output you get, and what output you expected to get. I mean, the findstr command on Windows is probably the tool with the worst regex support out of all regex tools that I know. So, in case you expect that \s will match a whitespace and \t will match a tab, then no, findstr does only support meta characters which are listed in the help message (findstr /?). And half of them are not even "standard regex".

You may want to have a look at JREPL viewtopic.php?f=3&t=6044

Steffen

Re: FINDSTR \s regexp meaning

Posted: 10 Jun 2021 11:15
by findstr
Well, due to localization, the results won't be of any particular use here.
ECHO /? | FINDSTR /R "\s"
returns a single string: ECHO [message]
What results do you get if you run ECHO /? | FINDSTR /R "\s"?

FOR /? | FINDSTR /R "\s"
returns some random strings from FOR's help.
DIR /? | FINDSTR /R "\s"
returns first 2 strings of documentation and 1 string from the middle: " sortorder N..."

What I'm trying to figure out is how exactly findstr interprets \s and is there any good use for \s.

Thanks for JREPL, I'll give it a try!

Re: FINDSTR \s regexp meaning

Posted: 10 Jun 2021 11:49
by T3RRY
findstr wrote:
10 Jun 2021 11:15
Well, due to localization, the results won't be of any particular use here.
ECHO /? | FINDSTR /R "\s"
returns a single string: ECHO [message]
What results do you get if you run ECHO /? | FINDSTR /R "\s"?

FOR /? | FINDSTR /R "\s"
returns some random strings from FOR's help.
DIR /? | FINDSTR /R "\s"
returns first 2 strings of documentation and 1 string from the middle: " sortorder N..."

What I'm trying to figure out is how exactly findstr interprets \s and is there any good use for \s.

Thanks for JREPL, I'll give it a try!
Had you taken the time to read the documentation of findstr, you would be aware using "\s" with the /R switch is equivalent to just using "s", given the backwards slash is the escape character for findstr meta characters. Given the "s" is just a regular character, the backslash is disregarded.

Re: FINDSTR \s regexp meaning

Posted: 10 Jun 2021 11:57
by aGerman
Yeah, also my expectation is that the backslash is simply ignored and findstr tries to match the s in each line.
Tested in a cmd window:

Code: Select all

D:\test>(echo qwe&echo asd&echo yxc)|findstr /r "\s"
asd

D:\test>
Certainly this thread on SO is more enlightening as to further behavior of findstr: https://stackoverflow.com/questions/884 ... str-comman

Steffen