使用单词分隔符正则表达式
我使用了特殊的\<
"单词开头"正则表达式符号。
我在Win10版本的findstr上尝试了这个方法。但据微软称,这个特殊的\<
符号从WinXP开始就一直存在于findstr.exe
中。
下面详细介绍了许多无法正常工作的选项,非常痛苦。
最底部是真正奏效的方法。
示例文件本身
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选项我们现在保留了空格,但是无法找到其他字符的情况。
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.
/r
选项来启用正则表达式吗? - Joey.
代替空格,因为大多数情况下我关心的是表达式中其他字母的位置,而空格是否真的是空格并不那么重要。findstr /n /r Load.frm *.*
(别误会,/c
很有用,但半数时间似乎对我不起作用)。 - ebyrob