更多相关内容...>>批处理 正则表达式(findstr) 整理
批处理 正则表达式(findstr) 整理
Findstr使用正则表达式搜索文件中的文本模式。
NnX6et http://blog.numino.net/
语法
cb5X64 http://blog.numino.net/
findstr [/e] [/l] [/r] [/s] [/i] [/x] [/v] [/n] [/m] [/o] [/p] [/offline] [/g:File] [/f:File] [/c:String] [/d:DirList] [/a:ColorAttribute] [Strings] [[Drive:][Path] FileName [...]]
P8K4uR http://blog.numino.net/
参数
8YJHK2 http://blog.numino.net/
/b
3s9nh5 http://blog.numino.net/
如果位于行的开头则匹配模式。
DSJ5Gz http://blog.numino.net/
/e
vOGAJz http://blog.numino.net/
如果位于行的末尾则匹配模式。
d9hG81 http://blog.numino.net/
/l
1rfKCO http://blog.numino.net/
逐字地搜索字符串。
1TfD6d http://blog.numino.net/
/r
i0WCDS http://blog.numino.net/
使用搜索串作为正则表达式。Findstr 将所有元字符解释为正则表达式,除非使用了 /l。
sfh0PZ http://blog.numino.net/
/s
YWfLsb http://blog.numino.net/
在当前目录和所有子目录中搜索匹配的文件。
FNEfdd http://blog.numino.net/
/i
C30xAn http://blog.numino.net/
指定搜索不区分大小写。
C9O3I9 http://blog.numino.net/
/x
eU1NI2 http://blog.numino.net/
打印完全匹配的行。
td7704 http://blog.numino.net/
/v
8pwC1z http://blog.numino.net/
仅打印不包含匹配的行。
G5GIG1 http://blog.numino.net/
/n
uJq8Nd http://blog.numino.net/
在每个匹配的行之前打印行号。
94g771 http://blog.numino.net/
/m
yNEpee http://blog.numino.net/
如果文件包含匹配项,则仅打印该文件名。
4NQqd9 http://blog.numino.net/
/o
eIGrk3 http://blog.numino.net/
在每个匹配行之前打印查找偏移量。
qk6jH0 http://blog.numino.net/
/p
Cm5TsL http://blog.numino.net/
跳过包含非可打印字符的文件。
YZzccj http://blog.numino.net/
/offline
RUm9t2 http://blog.numino.net/
利用脱机属性设置处理文件。
5d5sw8 http://blog.numino.net/
/f:File
KyG4Uu http://blog.numino.net/
从指定文件中读取文件列表。
N45CEy http://blog.numino.net/
/c:String
Z0N0Z4 http://blog.numino.net/
使用指定的文本作为文字搜索字符串。
J69rT7 http://blog.numino.net/
/g:File
XHYTcD http://blog.numino.net/
从指定文件得到搜索字符串。
INdRtS http://blog.numino.net/
/d:DirList
HemvZt http://blog.numino.net/
搜索以逗号分隔的目录列表。
U9aa32 http://blog.numino.net/
/a:ColorAttribute
y2iFb1 http://blog.numino.net/
使用两个十六进制数指定颜色属性。
yio70v http://blog.numino.net/
Strings
88KcN0 http://blog.numino.net/
指定要在 FileName 中搜索的文本。
z1Dj97 http://blog.numino.net/
[ Drive:][Path] FileName [...]
5C2H9Y http://blog.numino.net/
指定要搜索的文件。
EIhlxb http://blog.numino.net/
/?
hNwvd4 http://blog.numino.net/
在命令提示符下显示帮助。
5kgYMG http://blog.numino.net/
注释
M0u9fg http://blog.numino.net/
• 使用 Strings 和 [Drive:][Path] FileName [...]
0XwZc5 http://blog.numino.net/
在命令字符串中,所有 findstr 命令行选项必须在 Strings 和 [Drive:][Path] FileName [...] 之前。
6kwIVG http://blog.numino.net/
• 在 findstr 中使用正则表达式
Z9o76n http://blog.numino.net/
Findstr 可以在任何 ASCII 文件或文件中精确查找所要查找的文本。然而,有时要匹配的信息只有一部分或要查找更宽广的信息范围。在这种情况下,findstr 具有使用正则表达式搜索各种文本的强大功能。
be9Bub http://blog.numino.net/
正则表达式是用于指定文本类型的符号,与精确的字符串相反。标记使用文字字符和元字符。每个在常规的表达式语法中没有特殊意义的字符都是文字字符,与出现的该字符匹配。例如,字母和数字是文字符号。元字符是在正则表达式语法中具有特殊意义(操作符或分隔符)的符号。
oV26jT http://blog.numino.net/
下表列出 findstr 接受的元字符。
ihB2mk http://blog.numino.net/
字符 值
k9N0lf http://blog.numino.net/
.
xoTP35 http://blog.numino.net/
通配符:任何字符
nsXm1q http://blog.numino.net/
*
4q1395 http://blog.numino.net/
重复:以前零次或多次出现的字符或类
6OLAPj http://blog.numino.net/
^
G3xb7Y http://blog.numino.net/
行中的位置:行首
1NGZ2s http://blog.numino.net/
$
9BfsGt http://blog.numino.net/
行中的位置:行尾
9nB70Z http://blog.numino.net/
[class]
Nx9yBq http://blog.numino.net/
字符类:集中的任何一个字符
Astv8O http://blog.numino.net/
[^class]
U5Rzpa http://blog.numino.net/
反向类:非集中的任何一个字符
RokFQg http://blog.numino.net/
[X-y]
j1H5o0 http://blog.numino.net/
范围:指定范围内的任何字符
ncRe28 http://blog.numino.net/
\X
L2dlVr http://blog.numino.net/
转义:元字符 X 的文字用途
6ykU60 http://blog.numino.net/
\ 字的位置:字首
Z2njlx http://blog.numino.net/
xyz\>
8G9Slz http://blog.numino.net/
字的位置:字尾
D20xtL http://blog.numino.net/
组合使用正则表达式语法的特殊字符功能十分强大。例如,下面的通配符 (.) 和重复符 (*) 的组合可以匹配任何字符串:
GZxSre http://blog.numino.net/
.*
uepAoG http://blog.numino.net/
将如下表达式用作匹配以“b”开头并以“ing”结尾的任意字符串的更大表达式的组成部分:
Ndn6Gu http://blog.numino.net/
b.*ing
vVplC7 http://blog.numino.net/
示例
J6fR1I http://blog.numino.net/
使用空格分隔多个搜索字符串,除非参数以 /c 为前缀。要在文件 x.y 中搜索“hello”或“there”,请键入:
OELf4E http://blog.numino.net/
findstr "hello there" x.y
pXy9VH http://blog.numino.net/
要在文件 x.y 中搜索“hello there”,请键入:
gxIYZq http://blog.numino.net/
findstr /c:"hello there" x.y
yw10cx http://blog.numino.net/
若要查找文件 Proposal.txt 中出现的所有单词“Windows”(首字母 W 大写),请键入:
E26r37 http://blog.numino.net/
findstr Windows proposal.txt
Ip9JHi http://blog.numino.net/
若要搜索包含单词 Windows 的当前目录和所有子目录中的每个文件(不考虑字母大小写),请键入:
tpwJwJ http://blog.numino.net/
findstr /s /i Windows *.*
mPrU4s http://blog.numino.net/
要查找包含字“FOR”的所有行(前面可有任意空格,如:计算机程序中的循环),并包括每次出现的行号,请键入:
YgSmGl http://blog.numino.net/
findstr /b /n /c:"*FOR" *.bas
QSBIrr http://blog.numino.net/
如 果要在相同组文件中搜索几个不同项目,请在新行上创建包含每个搜索标准的文本文件。也可以列出要在文本文件中搜索的确切文件。要使用文件 Finddata.txt 中的搜索标准来搜索 Filelist.txt 中列出的文件,然后将结果保存到文件 Results.out 中,请键入:
0lwhtk http://blog.numino.net/
findstr /g:finddata.txt /f:filelist.txt > results.out
lkqW5s http://blog.numino.net/
假设您想要查找当前目录和所有子目录中包含单词“computer”的每个文件,而不考虑字母的大小写。若要列出包含单词“computer”的所有文件,请键入:
G9DNL3 http://blog.numino.net/
findstr /s /i /m "\" *.*
LQq8K6 http://blog.numino.net/
现在假定不仅需要查找单词“computer”,而且需要查找以字母“comp”开头的所有其他单词,例如“compliment”和“compete”。请键入:
Y2J2r7 http://blog.numino.net/
findstr /s /i /m "\
aA882Z http://blog.numino.net/
http://www.microsoft.com/technet/prodtechnol/windowsserver2003/zh-chs/library/ServerHelp/2b01d7f5-ab5a-407f-b5ec-f46248289db9.mspx?mfr=true
eR0k4w http://blog.numino.net/
实例:
9WhWOp http://blog.numino.net/
1.findstr . 2.txt 或 findstr "." 2.txt
7A3Eoj http://blog.numino.net/
从文件2.txt中查找任意字符,不包括空字符或空行
ffHw2o http://blog.numino.net/
====================
tsROCu http://blog.numino.net/
2.findstr .* 2.txt 或 findstr ".*" 2.txt
0LnDhF http://blog.numino.net/
从文件2.txt中查找任意字符包括空行和空字符
1PZumu http://blog.numino.net/
====================
14DoHj http://blog.numino.net/
3.findstr "[0-9]" 2.txt
6ptDio http://blog.numino.net/
从文件2.txt中查找包括数字0-9的字符串或行
5Wb3x0 http://blog.numino.net/
====================
dN6P4u http://blog.numino.net/
4.findstr "[a-zA-Z]" 2.txt
sE4Bf4 http://blog.numino.net/
从文件2.txt中查找包括任意字符的字符串或行
7ufP2w http://blog.numino.net/
====================
bYRGOT http://blog.numino.net/
5.findstr "[abcezy]" 2.txt
Uu0Ss0 http://blog.numino.net/
从文件2.txt中查找包括a b c e z y字母的字符串或行
xxLf04 http://blog.numino.net/
====================
SwmJR8 http://blog.numino.net/
6.findstr "[a-fl-z]" 2.txt
4c0iA9 http://blog.numino.net/
从文件2.txt中查找小写字符a-f l-z的字符串,但不包含g h I j k这几个字母。
OrUS4E http://blog.numino.net/
====================
9JxXrh http://blog.numino.net/
7.findstr "M[abc][hig]Y" 2.txt
Ovw9r5 http://blog.numino.net/
从文件2.txt中可以匹配 MahY , MbiY, MahY等…..
xh9Yt4 http://blog.numino.net/
====================
98dcYO http://blog.numino.net/
8. ^和$符号的应用
rQO5sz http://blog.numino.net/
^ 表示行首,"^step"仅匹配 "step hello world"中的第一个单词
cgOsLo http://blog.numino.net/
$ 表示行尾,"step$"仅匹配 "hello world step"中最后一个单词
cghzFD http://blog.numino.net/
====================
RA4T7K http://blog.numino.net/
9.finstr "[^0-9]" 2.txt
RoT9tF http://blog.numino.net/
如果是纯数字的字符串或者行便过滤掉,例如2323423423 这样的字符串,如果是345hh888这样的形式就不成了。
Ra3JNg http://blog.numino.net/
====================
ou5LDl http://blog.numino.net/
10.findstr "[^a-z]" 2.txt
eDoTks http://blog.numino.net/
同上,如果是纯字母的字符串或者行便过滤掉,例如 sdlfjlkjlksjdklfjlskdf这样的字符,如果是sdfksjdkf99999这样的形式,掺杂着数字就不成了
3m6XsM http://blog.numino.net/
====================
T18Sam http://blog.numino.net/
11.*号的作用
IPulzL http://blog.numino.net/
前面已经说过了 ".*"表示搜索的条件是任意字符,*号在正则表达式中的作用不是任何字符,而是表示左侧字符或者表达式的重复次数,*号表示重复的次数为零次或者多次。
0o5orI http://blog.numino.net/
====================
SJ22BT http://blog.numino.net/
12.findstr "^[0-9]*$" 2.txt
qJarXH http://blog.numino.net/
这个是匹配找到的纯数字,例如 234234234234,如果是2133234kkjl234就被过滤掉了。
28J0KV http://blog.numino.net/
Findstr "^[a-z]*$" 2.txt
7u5sRT http://blog.numino.net/
这个是匹配找到的纯字母,例如 sdfsdfsdfsdf,如果是213sldjfkljsdlk就被过滤掉了
xjpCW4 http://blog.numino.net/
如 果在搜索条件里没有*号,也就是说不重复左侧的搜索条件,也就是[0-9] [a-z]那只能匹配字符串的第一个字符也只有这一个字符,因为有行首和行尾的限制,"^[0-9]$"第一个字符如果是数字就匹配,如果不是就过滤掉, 如果字符串是 9 就匹配,如果是98或者9j之类的就不可以了。
DEwICE http://blog.numino.net/
=====================
bfV00E http://blog.numino.net/
13. "\<…\>"这个表达式的作用
0CteTj http://blog.numino.net/
这个表示精确查找一个字符串,\<sss 表示字的开始位置,sss\>表示字的结束位置
PD4I64 http://blog.numino.net/
echo hello world computer|findstr "\<computer\>"这样的形式
6z0P89 http://blog.numino.net/
echo hello worldcomputer|findstr "\<computer\>" 这样的形式就不成了,他要找的是 "computer"这个字符串,所以不可以。
4SvSG3 http://blog.numino.net/
echo hello worldcomputer|findstr ".*computer\>"这样就可以匹配了
更多相关内容...>>批处理 正则表达式(findstr) 整理

Bug报告 |  免责声明 |  联系我们 |  加入收藏

Copyright © 2006 NuminoStudio(www.numino.net) All Rights Reserved