0

I have read How to write a search pattern to include a space in findstr? and I thought the character class should solve my problem.

I came up with the following minimal reproducible example:

echo 1.999 K| findstr /R "\....[ ]K"

I expect \. to match the dot, ... to match 999, [ ] to match the space and K to match K. However, there's no output.

When I do

echo 1.999 K| findstr /R "\.... K"

it will be interpreted as 2 individual search terms, which I can't use, because I need the K to be after the number (compare to echo K 1.999 M| findstr /R "\.... K")

I'm on Windows 10 1903 18362.1016 if that matters.

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222

1 Answers1

2

Using /R /C:"\....[ ]K" works, as does /R /C:"\.... K". I've personally long given up trying to understand findstr's idiosyncrasies with parsing its arguments or regular expressions¹. If you don't have multiple search terms, using /C: should never do harm and only cut down on unexpected failures.

The other option, if you have it available, might be to pipe your data through PowerShell:

echo 1.999 K| powershell "$input -match '\....[ ]K'"
echo 1.999 K| pwsh -command  "$input -match '\....[ ]K'"

Slower (PowerShell 7 is quite a bit faster than 5.1), but a lot more predictable.


¹ dbenham has reverse-engineered findstr once, with a list of all known bugs. My guess here would be that despite the [], the space is still parsed as a separator between search terms, each of which then contain a literal bracket, leading to no match.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • It is hard to say that `findstr` supports using a regex given its limited abilities. PowerShell has much better regex support. – lit Aug 29 '20 at 20:38