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

时间:2022-07-17 13:53:22

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命令运行Windows 7。我试着:

  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:

所以我试着在Load和frm之间设置一个空白空间,然后给出如下命令:

 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?

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

4 个解决方案

#1


24  

If you use spaces, you need the /C: option to pass the the literal string(s) to the regex /R option.
Once the it gets to the regex, it's treated as a regex.

如果使用空格,则需要/C:选项将字符串(s)传递给regex /R选项。一旦它到达regex,它就被视为regex。

That said, this is typical MS trash.

也就是说,这是典型的垃圾女士。

Use two regex search strings

The bottom line is that you have to use 2 strings to handle cases where
Load frm is at the beginning like so:

最重要的是,你必须使用2个字符串来处理负载frm在开始时的情况:

  • Load frm apples bananas carrots
  • 装上苹果、香蕉、胡萝卜

OR in the middle like so:

或者在中间

  • some other text Load frm and more.
  • 其他一些文本加载frm等。

Version without character classes

Below is using XP sp3, windows 7 may be different, both are trash!

下面是使用XP sp3, windows 7可能不同,都是垃圾!

findstr /N /R /C:" *Load *frm" /C:"^Load *frm" test.txt

中/ N / R / C:“纳”*负载* / C:“^负载*农场”用法

7:Load frm is ok    
8:    Load     frm is ok  

Mind the Colon

NOTE: The colon in /C: is MANDATORY for this to work.

注意:/C中的冒号是必须的。

If you leave out the colon then findstr's error handling is just to treat /C as an invalid option, ignore that invalid option and go ahead anyway. Leading to unexpected and unwanted output.

如果您省略了冒号,那么findstr的错误处理只是将/C视为无效选项,忽略该无效选项,然后继续。导致意外和不必要的输出。

Equivalent version using character classes

findstr /N /R /C:"[ ][ ]*Load[ ][ ]*frm" /C:"^Load[ ][ ]*frm" test.txt

中/ N / R / C:“负载[][][][]* *农场”/ C:“^负载[][]*农场”用法

Character classes breakdown

// 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'

A real regex might be \bLoad\s+frm

真正的regex可能是\bLoad\s+frm

#2


18  

Use the /c option:

使用/ c选项:

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

From the help (findstr /?):

来自帮助(findstr /?):

/C:string  Uses specified string as a literal search string.

#3


1  

Use word delimiter regex

I used the the special \< "beginning of word" regex symbol.

我用了“word”的“开头”的特殊符号。

I tried this on the Win10 version of findstr. But according to Microsoft this special \< symbol has been in findstr.exe ever since WinXP.

我在Win10版本的findstr中尝试过这个方法。但是根据微软的说法,这个特殊的\ <符号出现在findstr中。exe自从winxp。< p>

Full (and painful) breakdown of many options that do NOT work below.

完全(并且痛苦地)分解了许多在下面不起作用的选项。

At the very bottom: what actually worked.

最根本的问题是:真正起作用的是什么?

The sample file itself

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.

Wrong. With regular execution space is treated as delimiter.

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.

Wrong: With Regex option space is STILL treated as delimiter.

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.    

More right but still wrong. With /C option we now get preserved spaces but don't find other character cases.

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.

Wrong. /I for "Ignore Case" does not help. We get matches from within words we did not want.

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.

Right. Use special "Beginning of word" regex symbol. Matches beginning-of-line or space.

Either case sensitive:

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.

or ignoring case

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.

#4


0  

This piece of code will only allow letters, numbers, underscore and white space in keyword:

这段代码只允许在关键字中使用字母、数字、下划线和空格:

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

#1


24  

If you use spaces, you need the /C: option to pass the the literal string(s) to the regex /R option.
Once the it gets to the regex, it's treated as a regex.

如果使用空格,则需要/C:选项将字符串(s)传递给regex /R选项。一旦它到达regex,它就被视为regex。

That said, this is typical MS trash.

也就是说,这是典型的垃圾女士。

Use two regex search strings

The bottom line is that you have to use 2 strings to handle cases where
Load frm is at the beginning like so:

最重要的是,你必须使用2个字符串来处理负载frm在开始时的情况:

  • Load frm apples bananas carrots
  • 装上苹果、香蕉、胡萝卜

OR in the middle like so:

或者在中间

  • some other text Load frm and more.
  • 其他一些文本加载frm等。

Version without character classes

Below is using XP sp3, windows 7 may be different, both are trash!

下面是使用XP sp3, windows 7可能不同,都是垃圾!

findstr /N /R /C:" *Load *frm" /C:"^Load *frm" test.txt

中/ N / R / C:“纳”*负载* / C:“^负载*农场”用法

7:Load frm is ok    
8:    Load     frm is ok  

Mind the Colon

NOTE: The colon in /C: is MANDATORY for this to work.

注意:/C中的冒号是必须的。

If you leave out the colon then findstr's error handling is just to treat /C as an invalid option, ignore that invalid option and go ahead anyway. Leading to unexpected and unwanted output.

如果您省略了冒号,那么findstr的错误处理只是将/C视为无效选项,忽略该无效选项,然后继续。导致意外和不必要的输出。

Equivalent version using character classes

findstr /N /R /C:"[ ][ ]*Load[ ][ ]*frm" /C:"^Load[ ][ ]*frm" test.txt

中/ N / R / C:“负载[][][][]* *农场”/ C:“^负载[][]*农场”用法

Character classes breakdown

// 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'

A real regex might be \bLoad\s+frm

真正的regex可能是\bLoad\s+frm

#2


18  

Use the /c option:

使用/ c选项:

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

From the help (findstr /?):

来自帮助(findstr /?):

/C:string  Uses specified string as a literal search string.

#3


1  

Use word delimiter regex

I used the the special \< "beginning of word" regex symbol.

我用了“word”的“开头”的特殊符号。

I tried this on the Win10 version of findstr. But according to Microsoft this special \< symbol has been in findstr.exe ever since WinXP.

我在Win10版本的findstr中尝试过这个方法。但是根据微软的说法,这个特殊的\ <符号出现在findstr中。exe自从winxp。< p>

Full (and painful) breakdown of many options that do NOT work below.

完全(并且痛苦地)分解了许多在下面不起作用的选项。

At the very bottom: what actually worked.

最根本的问题是:真正起作用的是什么?

The sample file itself

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.

Wrong. With regular execution space is treated as delimiter.

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.

Wrong: With Regex option space is STILL treated as delimiter.

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.    

More right but still wrong. With /C option we now get preserved spaces but don't find other character cases.

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.

Wrong. /I for "Ignore Case" does not help. We get matches from within words we did not want.

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.

Right. Use special "Beginning of word" regex symbol. Matches beginning-of-line or space.

Either case sensitive:

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.

or ignoring case

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.

#4


0  

This piece of code will only allow letters, numbers, underscore and white space in keyword:

这段代码只允许在关键字中使用字母、数字、下划线和空格:

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