如何编写搜索模式以在findstr中包含空格?

Cod*_*lue 33 regex findstr

我想搜索某个目录中的所有文件以查找语句的出现

  Load frmXYZ
Run Code Online (Sandbox Code Playgroud)

我在Windows 7上使用该findstr命令.我试过了:

  findstr /n Load.*frm *.*
Run Code Online (Sandbox Code Playgroud)

但这给了我不想要的结果,例如:

 If ABCFormLoaded Then Unload frmPQR
Run Code Online (Sandbox Code Playgroud)

所以我试图在它之间放一个空格Load,frm并给出这样的命令:

 findstr /n Load frm *.*
Run Code Online (Sandbox Code Playgroud)

但这只是搜索了所有单词load或所有单词的出现次数frm.我该如何解决这个问题?

小智 30

如果使用空格,则需要/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  
Run Code Online (Sandbox Code Playgroud)

介意冒号

注意:冒号/C:是MANDATORY,以便工作.

如果你省略冒号,那么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'
Run Code Online (Sandbox Code Playgroud)

一个真正的正则表达式可能是 \bLoad\s+frm

  • 哦,我认为这是一个技术术语! (2认同)
  • @TranslucentCloud - 甚至必须处理垃圾.嘿,看看那个,它是垃圾:`必须使用2个字符串来处理'Load frm'位于开头或中间的情况 (2认同)

Joe*_*oey 24

使用/c选项:

findstr /n /c:"Load frm" *.*
Run Code Online (Sandbox Code Playgroud)

来自help(findstr /?):

/C:string  Uses specified string as a literal search string.
Run Code Online (Sandbox Code Playgroud)

  • 好吧,然后使用`[ ][ ]*`。正则表达式并不严格需要`+`。 (2认同)

Sta*_*uff 6

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

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

我在 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.
Run Code Online (Sandbox Code Playgroud)

错误的。与常规执行空间被视为分隔符。

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.
Run Code Online (Sandbox Code Playgroud)

错误:使用正则表达式选项空间仍然被视为分隔符。

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.    
Run Code Online (Sandbox Code Playgroud)

更对了,但还是错了。使用 /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.
Run Code Online (Sandbox Code Playgroud)

错误的。/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.
Run Code Online (Sandbox Code Playgroud)

对。使用特殊的“词首”正则表达式符号。匹配行首或空格。

区分大小写:

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.
Run Code Online (Sandbox Code Playgroud)

或忽略大小写

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.
Run Code Online (Sandbox Code Playgroud)