不使用 findstr 对正则表达式进行操作将不起作用
Not operation on a regex using findstr won't work
我有一个文件test_file.txt
test
test_1
我使用 findstr 查找字符串
C:\Users\Rafael\Desktop>findstr /R /C:"test" test_file.txt
test
test_1
如果我使用下划线查找字符串,它会起作用
C:\Users\Rafael\Desktop>findstr /R /C:"test[_]" test_file.txt
test_1
但是,如果我查找不使用下划线的字符串,我将一无所获。
C:\Users\Rafael\Desktop>findstr /R /C:"test[^_]" test_file.txt
C:\Users\Rafael\Desktop>
使用regex101.com:
为什么?有解决办法吗?
test[^_]
是一个错误的正则表达式,只用 test
来捕捉行,因为 [^_]
需要 一个字符但是有 none:字符串在 test
.
之后结束
应使用单词边界特殊字符:
findstr /r /c:"\<test\>" inputfile.txt
我有一个文件test_file.txt
test
test_1
我使用 findstr 查找字符串
C:\Users\Rafael\Desktop>findstr /R /C:"test" test_file.txt
test
test_1
如果我使用下划线查找字符串,它会起作用
C:\Users\Rafael\Desktop>findstr /R /C:"test[_]" test_file.txt
test_1
但是,如果我查找不使用下划线的字符串,我将一无所获。
C:\Users\Rafael\Desktop>findstr /R /C:"test[^_]" test_file.txt
C:\Users\Rafael\Desktop>
使用regex101.com:
为什么?有解决办法吗?
test[^_]
是一个错误的正则表达式,只用 test
来捕捉行,因为 [^_]
需要 一个字符但是有 none:字符串在 test
.
应使用单词边界特殊字符:
findstr /r /c:"\<test\>" inputfile.txt