批处理FINDSTR正则表达式用法实例分析

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\>"这样就可以匹配了

( ! ) 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
#TimeMemoryFunctionLocation
10.0019412824{main}( ).../index.php:0
20.08934263224Joomla\CMS\Application\SiteApplication->execute( ).../index.php:49
30.08934263224Joomla\CMS\Application\SiteApplication->doExecute( ).../CMSApplication.php:196
40.310311454224Joomla\CMS\Application\SiteApplication->dispatch( ).../SiteApplication.php:233
50.311211478856Joomla\CMS\Component\ComponentHelper::renderComponent( ).../SiteApplication.php:194
60.312411534184Joomla\CMS\Component\ComponentHelper::executeComponent( ).../ComponentHelper.php:377
70.312711561584require_once( '/var/www/vhosts/shan.info/httpdocs/components/com_k2/k2.php' ).../ComponentHelper.php:402
80.323811961216K2ControllerItem->execute( ).../k2.php:64
90.323811961216K2ControllerItem->display( ).../BaseController.php:710
100.337412611920K2ControllerItem->display( ).../item.php:78
110.337412611920K2ControllerItem->display( ).../controller.php:19
120.343712982960Joomla\CMS\Cache\Controller\ViewController->get( ).../BaseController.php:663
130.345013003328K2ViewItem->display( ).../ViewController.php:102
140.413715835792K2ViewItem->display( ).../view.html.php:742
150.413715835792K2ViewItem->loadTemplate( ).../HtmlView.php:230
160.416016008728include( '/var/www/vhosts/shan.info/httpdocs/templates/gk_publisher/html/com_k2/templates/default/item.php' ).../HtmlView.php:701

( ! ) 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
#TimeMemoryFunctionLocation
10.0019412824{main}( ).../index.php:0
20.08934263224Joomla\CMS\Application\SiteApplication->execute( ).../index.php:49
30.08934263224Joomla\CMS\Application\SiteApplication->doExecute( ).../CMSApplication.php:196
40.310311454224Joomla\CMS\Application\SiteApplication->dispatch( ).../SiteApplication.php:233
50.311211478856Joomla\CMS\Component\ComponentHelper::renderComponent( ).../SiteApplication.php:194
60.312411534184Joomla\CMS\Component\ComponentHelper::executeComponent( ).../ComponentHelper.php:377
70.312711561584require_once( '/var/www/vhosts/shan.info/httpdocs/components/com_k2/k2.php' ).../ComponentHelper.php:402
80.323811961216K2ControllerItem->execute( ).../k2.php:64
90.323811961216K2ControllerItem->display( ).../BaseController.php:710
100.337412611920K2ControllerItem->display( ).../item.php:78
110.337412611920K2ControllerItem->display( ).../controller.php:19
120.343712982960Joomla\CMS\Cache\Controller\ViewController->get( ).../BaseController.php:663
130.345013003328K2ViewItem->display( ).../ViewController.php:102
140.413715835792K2ViewItem->display( ).../view.html.php:742
150.413715835792K2ViewItem->loadTemplate( ).../HtmlView.php:230
160.416016008728include( '/var/www/vhosts/shan.info/httpdocs/templates/gk_publisher/html/com_k2/templates/default/item.php' ).../HtmlView.php:701
back to top