Search code examples
regexwindowsbatch-filecmdfindstr

Not operation on a regex using findstr won't work


I have a file test_file.txt

test
test_1

I use findstr to look for a string

C:\Users\Rafael\Desktop>findstr /R /C:"test" test_file.txt
test
test_1

If I look for a string using underscore, it works

C:\Users\Rafael\Desktop>findstr /R /C:"test[_]" test_file.txt
test_1

However, if I look for a string NOT using underscore, I've got nothing.

C:\Users\Rafael\Desktop>findstr /R /C:"test[^_]" test_file.txt

C:\Users\Rafael\Desktop>

Using regex101.com:

Test working using regex101.com

Why? Is there a solution?


Solution

  • test[^_] is a wrong regexp to catch the line with only test because [^_] needs a character but there's none: the string ends right after test.

    Word boundary special characters should be used:

    findstr /r /c:"\<test\>" inputfile.txt