如何编写搜索模式以在 findstr 中包含空格? [英] How to write a search pattern to include a space in findstr?
问题描述
我想在某个目录中的所有文件中搜索诸如
I want to search all files in a certain directory for occurrences of statements such as
Load frmXYZ
我使用的是 Windows 7,使用 findstr
命令.我试过了:
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
所以我尝试在 Load
和 frm
之间放置一个空格并给出如下命令:
So I tried to put a blankspace between Load
and frm
and gave the command like this:
findstr /n Load frm *.*
但这只是搜索所有出现的词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?
推荐答案
如果使用空格,则需要 /C:
选项将文字字符串传递给正则表达式 /R
选项.
一旦它到达正则表达式,它就会被视为正则表达式.
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.
也就是说,这是典型的 MS 垃圾.
That said, this is typical MS trash.
最重要的是,您必须使用 2 个字符串来处理以下情况Load frm
开头是这样的:
The bottom line is that you have to use 2 strings to handle cases where
Load frm
is at the beginning like so:
加载苹果香蕉胡萝卜
或在中间像这样:
一些其他文本加载 frm 和更多
.
下面是XP sp3,windows 7可能不一样,都是垃圾!
Below is using XP sp3, windows 7 may be different, both are trash!
findstr/N/R/C:" *Load *frm"/C:"^Load *frm" test.txt
7:Load frm is ok
8: Load frm is ok
注意冒号
注意:/C:
中的冒号是强制执行的.
Mind the Colon
NOTE: The colon in /C:
is MANDATORY for this to work.
如果省略冒号,则 findstr
的错误处理只是将 /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/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
A real regex might be \bLoad\s+frm
这篇关于如何编写搜索模式以在 findstr 中包含空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!