批处理FINDSTR正则表达式用法实例分析
- font size decrease font size increase font size
- Print Email
dos或批处理下findstr正则用法,会了这个我们就可以用批处理实现文本等搜索替换等
1.findstr . 2.txt 或 Findstr "." 2.txt
从文件2.txt中查找任意字符,不包括空字符或空行
2.findstr .* 2.txt 或 findstr ".*" 2.txt
从文件2.txt中查找任意字符包括空行和空字符
3.findstr "[0-9]" 2.txt
从文件2.txt中查找包括数字0-9的字符串或行
4.findstr "[a-zA-Z]" 2.txt
从文件2.txt中查找包括任意字符的字符串或行
5.findstr "[abcezy]" 2.txt
从文件2.txt中查找包括a b c e z y字母的字符串或行
6.findstr "[a-fl-z]" 2.txt
从文件2.txt中查找小写字符a-f l-z的字符串,但不包含g h I j k这几个字母。
7.findstr "M[abc][hig]Y" 2.txt
从文件2.txt中可以匹配 MahY , MbiY, MahY等…..
8. ^和$符号的应用
^ 表示行首,"^step"仅匹配 "step hello world"中的第一个单词
$ 表示行尾,"step$"仅匹配 "hello world step"中最后一个单词
9.finstr "[^0-9]" 2.txt
如果是纯数字的字符串或者行便过滤掉,例如2323423423 这样的字符串,如果是345hh888这样的形式就不成了。
10.findstr "[^a-z]" 2.txt
同上,如果是纯字母的字符串或者行便过滤掉,例如 sdlfjlkjlksjdklfjlskdf这样的字符,如果是sdfksjdkf99999这样的形式,掺杂着数字就不成了
11.*号的作用
前面已经说过了 ".*"表示搜索的条件是任意字符,*号在正则表达式中的作用不是任何字符,而是表示左侧字符或者表达式的重复次数,*号表示重复的次数为零次或者多次。
12.findstr "^[0-9]*$" 2.txt
这个是匹配找到的纯数字,例如 234234234234,如果是2133234kkjl234就被过滤掉了。
Findstr "^[a-z]*$" 2.txt
这个是匹配找到的纯字母,例如 sdfsdfsdfsdf,如果是213sldjfkljsdlk就被过滤掉了
如果在搜索条件里没有*号,也就是说不重复左侧的搜索条件,也就是[0-9] [a-z]那只能匹配字符串的第一个字符也只有这一个字符,因为有行首和行尾的限制,"^[0-9]$"第一个字符如果是数字就匹配,如果不是就过滤掉,如果字符串是 9 就匹配,如果是98或者9j之类的就不可以了。
13. "\<…\>"这个表达式的作用
这个表示精确查找一个字符串,\<sss 表示字的开始位置,sss\>表示字的结束位置
echo hello world computer|findstr "\<computer\>"这样的形式
echo hello worldcomputer|findstr "\<computer\>" 这样的形式就不成了,他要找的是 "computer"这个字符串,所以不可以。
echo hello worldcomputer|findstr ".*computer\>"这样就可以匹配了
从文件2.txt中查找任意字符,不包括空字符或空行
2.findstr .* 2.txt 或 findstr ".*" 2.txt
从文件2.txt中查找任意字符包括空行和空字符
3.findstr "[0-9]" 2.txt
从文件2.txt中查找包括数字0-9的字符串或行
4.findstr "[a-zA-Z]" 2.txt
从文件2.txt中查找包括任意字符的字符串或行
5.findstr "[abcezy]" 2.txt
从文件2.txt中查找包括a b c e z y字母的字符串或行
6.findstr "[a-fl-z]" 2.txt
从文件2.txt中查找小写字符a-f l-z的字符串,但不包含g h I j k这几个字母。
7.findstr "M[abc][hig]Y" 2.txt
从文件2.txt中可以匹配 MahY , MbiY, MahY等…..
8. ^和$符号的应用
^ 表示行首,"^step"仅匹配 "step hello world"中的第一个单词
$ 表示行尾,"step$"仅匹配 "hello world step"中最后一个单词
9.finstr "[^0-9]" 2.txt
如果是纯数字的字符串或者行便过滤掉,例如2323423423 这样的字符串,如果是345hh888这样的形式就不成了。
10.findstr "[^a-z]" 2.txt
同上,如果是纯字母的字符串或者行便过滤掉,例如 sdlfjlkjlksjdklfjlskdf这样的字符,如果是sdfksjdkf99999这样的形式,掺杂着数字就不成了
11.*号的作用
前面已经说过了 ".*"表示搜索的条件是任意字符,*号在正则表达式中的作用不是任何字符,而是表示左侧字符或者表达式的重复次数,*号表示重复的次数为零次或者多次。
12.findstr "^[0-9]*$" 2.txt
这个是匹配找到的纯数字,例如 234234234234,如果是2133234kkjl234就被过滤掉了。
Findstr "^[a-z]*$" 2.txt
这个是匹配找到的纯字母,例如 sdfsdfsdfsdf,如果是213sldjfkljsdlk就被过滤掉了
如果在搜索条件里没有*号,也就是说不重复左侧的搜索条件,也就是[0-9] [a-z]那只能匹配字符串的第一个字符也只有这一个字符,因为有行首和行尾的限制,"^[0-9]$"第一个字符如果是数字就匹配,如果不是就过滤掉,如果字符串是 9 就匹配,如果是98或者9j之类的就不可以了。
13. "\<…\>"这个表达式的作用
这个表示精确查找一个字符串,\<sss 表示字的开始位置,sss\>表示字的结束位置
echo hello world computer|findstr "\<computer\>"这样的形式
echo hello worldcomputer|findstr "\<computer\>" 这样的形式就不成了,他要找的是 "computer"这个字符串,所以不可以。
echo hello worldcomputer|findstr ".*computer\>"这样就可以匹配了
( ! ) Warning: count(): Parameter must be an array or an object that implements Countable in /var/www/vhosts/shan.info/httpdocs/templates/gk_publisher/html/com_k2/templates/default/item.php on line 169 | ||||
---|---|---|---|---|
Call Stack | ||||
# | Time | Memory | Function | Location |
1 | 0.0019 | 412824 | {main}( ) | .../index.php:0 |
2 | 0.0893 | 4263224 | Joomla\CMS\Application\SiteApplication->execute( ) | .../index.php:49 |
3 | 0.0893 | 4263224 | Joomla\CMS\Application\SiteApplication->doExecute( ) | .../CMSApplication.php:196 |
4 | 0.3103 | 11454224 | Joomla\CMS\Application\SiteApplication->dispatch( ) | .../SiteApplication.php:233 |
5 | 0.3112 | 11478856 | Joomla\CMS\Component\ComponentHelper::renderComponent( ) | .../SiteApplication.php:194 |
6 | 0.3124 | 11534184 | Joomla\CMS\Component\ComponentHelper::executeComponent( ) | .../ComponentHelper.php:377 |
7 | 0.3127 | 11561584 | require_once( '/var/www/vhosts/shan.info/httpdocs/components/com_k2/k2.php' ) | .../ComponentHelper.php:402 |
8 | 0.3238 | 11961216 | K2ControllerItem->execute( ) | .../k2.php:64 |
9 | 0.3238 | 11961216 | K2ControllerItem->display( ) | .../BaseController.php:710 |
10 | 0.3374 | 12611920 | K2ControllerItem->display( ) | .../item.php:78 |
11 | 0.3374 | 12611920 | K2ControllerItem->display( ) | .../controller.php:19 |
12 | 0.3437 | 12982960 | Joomla\CMS\Cache\Controller\ViewController->get( ) | .../BaseController.php:663 |
13 | 0.3450 | 13003328 | K2ViewItem->display( ) | .../ViewController.php:102 |
14 | 0.4137 | 15835792 | K2ViewItem->display( ) | .../view.html.php:742 |
15 | 0.4137 | 15835792 | K2ViewItem->loadTemplate( ) | .../HtmlView.php:230 |
16 | 0.4160 | 16008728 | include( '/var/www/vhosts/shan.info/httpdocs/templates/gk_publisher/html/com_k2/templates/default/item.php' ) | .../HtmlView.php:701 |
- Published in Windows 7
- Read 2957 times
( ! ) Notice: Only variables should be assigned by reference in /var/www/vhosts/shan.info/httpdocs/templates/gk_publisher/html/com_k2/templates/default/item.php on line 478 | ||||
---|---|---|---|---|
Call Stack | ||||
# | Time | Memory | Function | Location |
1 | 0.0019 | 412824 | {main}( ) | .../index.php:0 |
2 | 0.0893 | 4263224 | Joomla\CMS\Application\SiteApplication->execute( ) | .../index.php:49 |
3 | 0.0893 | 4263224 | Joomla\CMS\Application\SiteApplication->doExecute( ) | .../CMSApplication.php:196 |
4 | 0.3103 | 11454224 | Joomla\CMS\Application\SiteApplication->dispatch( ) | .../SiteApplication.php:233 |
5 | 0.3112 | 11478856 | Joomla\CMS\Component\ComponentHelper::renderComponent( ) | .../SiteApplication.php:194 |
6 | 0.3124 | 11534184 | Joomla\CMS\Component\ComponentHelper::executeComponent( ) | .../ComponentHelper.php:377 |
7 | 0.3127 | 11561584 | require_once( '/var/www/vhosts/shan.info/httpdocs/components/com_k2/k2.php' ) | .../ComponentHelper.php:402 |
8 | 0.3238 | 11961216 | K2ControllerItem->execute( ) | .../k2.php:64 |
9 | 0.3238 | 11961216 | K2ControllerItem->display( ) | .../BaseController.php:710 |
10 | 0.3374 | 12611920 | K2ControllerItem->display( ) | .../item.php:78 |
11 | 0.3374 | 12611920 | K2ControllerItem->display( ) | .../controller.php:19 |
12 | 0.3437 | 12982960 | Joomla\CMS\Cache\Controller\ViewController->get( ) | .../BaseController.php:663 |
13 | 0.3450 | 13003328 | K2ViewItem->display( ) | .../ViewController.php:102 |
14 | 0.4137 | 15835792 | K2ViewItem->display( ) | .../view.html.php:742 |
15 | 0.4137 | 15835792 | K2ViewItem->loadTemplate( ) | .../HtmlView.php:230 |
16 | 0.4160 | 16008728 | include( '/var/www/vhosts/shan.info/httpdocs/templates/gk_publisher/html/com_k2/templates/default/item.php' ) | .../HtmlView.php:701 |