如何在 windows 批处理文件中使用正则表达式查找特定模式?

How to find particular pattern with regex in windows batch file?

要求: 解析文本文件并需要将特定的行模式提取到另一个 file.In 输出文件我只需要带有日期的行后跟 ERROR 。

批处理文件代码:

findstr /r "^\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2},\d{3} \<ERROR.*" server.log > "%CD%"/test.txt
pause

输入文件: Sample.log

2017-11-28 00:40:16,791 ERROR [org.jboss.weld.Bean] (ServerService Thread Pool -- 269) WELD-000019: Error destroying an instance 
2017-11-28 00:40:16,791 INFO  [stdout] (ServerService Thread Pool -- 269) [ERROR][faces context is null in context utils1]
2017-11-28 00:40:16,791 INFO  [stdout] (ServerService Thread Pool -- 269) [ERROR][faces context is null in context utils1]
2017-11-28 00:40:16,791 ERROR [org.jboss.weld.Bean] (ServerService Thread Pool -- 269) WELD-000019: Error destroying an instance
2017-11-28 00:40:16,791 INFO  [stdout] (ServerService Thread Pool -- 269) [ERROR][faces context is null in context utils1]
2017-11-28 00:40:16,791 INFO  [stdout] (ServerService Thread Pool -- 269) [ERROR][faces context is null in context utils1]

预期输出:

2017-11-28 00:40:16,791 ERROR [org.jboss.weld.Bean] (ServerService Thread Pool -- 269) WELD-000019: Error destroying an instance 
2017-11-28 00:40:16,791 ERROR [org.jboss.weld.Bean] (ServerService Thread Pool -- 269) WELD-000019: Error destroying an instance 

但目前代码return输出文件中的所有内容。 也试过字边界\。

FINDSTR /b /r "....-..-.....:..:..,....ERROR" "%filename1%

其中 filename1 包含您的源文件名应该可以完成任务,因为 findstr 对正则表达式的实施受到限制。