37

I want to search all files in a certain directory for occurrences of statements such as

  Load frmXYZ

I am on Windows 7, using the findstr command. I tried:

  findstr /n Load.*frm *.*

But this gives me unwanted results such as:

 If ABCFormLoaded Then Unload frmPQR

So I tried to put a blankspace between Load and frm and gave the command like this:

 findstr /n Load frm *.*

But this simply searched for all occurrences of the word load or all occurrences of the word frm. How do I get around this problem?

4

4 回答 4

40

如果您使用空格,则需要/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

于 2012-03-20T17:01:16.510 回答
31

使用/c选项:

findstr /n /c:"Load frm" *.*

从帮助(findstr /?):

/C:string  Uses specified string as a literal search string.
于 2012-03-20T15:19:21.147 回答
8

使用单词分隔符正则表达式

我使用了特殊\<的“单词开头”正则表达式符号。

我在findstr的Win10版本上试过这个。但根据微软的说法,这个特殊\<符号findstr.exe自 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.

错误:使用 Regex 选项,空间仍被视为分隔符。

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 选项,我们现在可以保留空格,但找不到其他字符大小写。

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.

错误的。/I 表示“忽略大小写”没有帮助。我们从我们不想要的单词中得到匹配。

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.
于 2017-03-01T14:22:57.003 回答
-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
于 2015-04-30T11:19:24.970 回答