我想在某个目录中搜索所有文件,例如
Load frmXYZ
我使用findstr
命令在Windows 7上。我试过了:
findstr /n Load.*frm *.*
但是这给了我不想要的结果,例如:
If ABCFormLoaded Then Unload frmPQR
所以我试图在Load
和frm
之间放置一个空格,并给出如下命令:
findstr /n Load frm *.*
但这只是搜索单词load
的所有出现或单词frm
的所有出现。我该如何解决这个问题?
答案 0 :(得分:27)
如果使用空格,则需要/C:
选项将文字字符串传递给正则表达式/R
选项。
一旦它进入正则表达式,它就被视为正则表达式。
那就是说,这是典型的MS垃圾。
最重要的是你必须使用2个字符串来处理
的情况
Load frm
就像这样:
Load frm apples bananas carrots
或者在中间就像这样:
some other text Load frm and more
。下面是使用XP sp3,windows 7可能会有所不同,两者都是垃圾!
findstr /N /R /C:" *Load *frm" /C:"^Load *frm" test.txt
7:Load frm is ok
8: Load frm is ok
注意: /C:
中的冒号是强制性的,可以使其生效。
如果你遗漏冒号,那么findstr
的错误处理只是将/C
视为无效选项,忽略该无效选项并继续进行。导致意外和不需要的输出。
findstr /N /R /C:"[ ][ ]*Load[ ][ ]*frm" /C:"^Load[ ][ ]*frm" test.txt
// The first regex search string breaks down like this:
[ ] // require 1 space
[ ]* // optional many spaces
Load // literal 'Load'
[ ] // require 1 space
[ ]* // optional many spaces
frm // literal 'frm'
// The second regex search string breaks down like this:
^ // beginning of line
Load // literal 'Load'
[ ] // require 1 space
[ ]* // optional many spaces
frm // literal 'frm'
真正的正则表达式可能是\bLoad\s+frm
答案 1 :(得分:23)
使用/c
选项:
findstr /n /c:"Load frm" *.*
来自帮助(findstr /?
):
/C:string Uses specified string as a literal search string.
答案 2 :(得分:3)
我使用了特殊的\<
&#34;词的开头&#34;正则表达式符号。
我在Findtr的Win10版本上试过这个。但根据微软的特殊\<
symbol has been in findstr.exe
ever since WinXP。
在下面不起作用的许多选项的完整(和痛苦)细分。
在最底层:实际工作的内容。
C:\>type lines.txt
Load frmXYZ // This line should match.
If ABCFormLoaded Then Unload frmPQR // This line should NOT match.
pears Load frm grapes pineapples // This line should match.
// This blank line should NOT match.
LOAD FRMXYZ // This line should match.
IF ABCFORMLOADED THEN UNLOAD FRMPQR // This line should NOT match.
PEARS LOAD FRM GRAPES PINEAPPLES // This line should match.
// This blank line should NOT match.
load frmxyz // This line should match.
if abcformloaded then unload frmpqr // This line should NOT match.
pears load frm grapes pineapples // This line should match.
C:\>type lines.txt | findstr /N "Load frm"
1:Load frmXYZ // This line should match.
2:If ABCFormLoaded Then Unload frmPQR // This line should NOT match.
3:pears Load frm grapes pineapples // This line should match.
9:load frmxyz // This line should match.
10:if abcformloaded then unload frmpqr // This line should NOT match.
11:pears load frm grapes pineapples // This line should match.
C:\>type lines.txt | findstr /N /R "Load frm"
1:Load frmXYZ // This line should match.
2:If ABCFormLoaded Then Unload frmPQR // This line should NOT match.
3:pears Load frm grapes pineapples // This line should match.
9:load frmxyz // This line should match.
10:if abcformloaded then unload frmpqr // This line should NOT match.
11:pears load frm grapes pineapples // This line should match.
C:\>type lines.txt | findstr /N /R /C:"Load frm"
1:Load frmXYZ // This line should match.
3:pears Load frm grapes pineapples // This line should match.
C:\>type lines.txt | findstr /N /R /I /C:"Load frm"
1:Load frmXYZ // This line should match.
2:If ABCFormLoaded Then Unload frmPQR // This line should NOT match.
3:pears Load frm grapes pineapples // This line should match.
5:LOAD FRMXYZ // This line should match.
6:IF ABCFORMLOADED THEN UNLOAD FRMPQR // This line should NOT match.
7:PEARS LOAD FRM GRAPES PINEAPPLES // This line should match.
9:load frmxyz // This line should match.
10:if abcformloaded then unload frmpqr // This line should NOT match.
11:pears load frm grapes pineapples // This line should match.
C:\>type lines.txt | findstr /N /R /C:"\<Load frm"
1:Load frmXYZ // This line should match.
3:pears Load frm grapes pineapples // This line should match.
C:\>type lines.txt | findstr /N /R /I /C:"\<Load frm"
1:Load frmXYZ // This line should match.
3:pears Load frm grapes pineapples // This line should match.
5:LOAD FRMXYZ // This line should match.
7:PEARS LOAD FRM GRAPES PINEAPPLES // This line should match.
9:load frmxyz // This line should match.
11:pears load frm grapes pineapples // This line should match.
答案 3 :(得分:-1)
这段代码只允许使用关键字中的字母,数字,下划线和空格:
set /p keyword="Enter keyword: " || Set keyword=
set keyword_replaced=%keyword: =_%
echo %keyword_replaced%| findstr /r "[^0-9a-zA-Z_]" > nul
if errorlevel 1 goto noexit
echo special characters in keyword not allowed (except space and _), TERMINATING
timeout 4
exit /b 0
:noexit